This is the 24th day of my participation in the August More Text Challenge

Hi, I’m Ken

Author: Please call me Ken Link: juejin.cn/user/109118… The copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please indicate the source.

🌊🌈

Part of the content and pictures of the article are from the Internet. If you have any questions, please contact me (there is an official account in the introduction of the home page).

This blog is suitable for just contactJSAnd the friends who want to review after a long time.

🌊🌈

5.4_ Date objects

Date objects in JavaScript are used to handle dates and times. For example, the real-time display of dates in seckill activities, clock effects, online calendars, etc.

5.4.1_ Use of date objects

Date objects in JavaScript need to be instantiated using new Date(), which is the Date object’s constructor. When creating a Date object, you can pass parameters to the Date() constructor to represent a specific Date as follows.

// Method 1: No parameter, use the current time of the current system as the time to save the object
var datel = new Date(a);Wed 0CT 16 2019 10:57:56 GMT+0800 (UTC)
console.log (datel);

// Method 2: Year, month, day, hour, minute, second (month ranges from 0 to 11, i.e. real month -1)
var date2 = new Date(2019.10.16.10.57.56);
Sat Nov 16 2019 10:57:56 GMT+0800
console.log(date2);

// Method 3: Use a string to represent the date and time
var date3 = new Date('the 2019-10-16-10:57:56');
Wed Oct 16 2019 10:57:56 GMT+0800
console.log(date3);
Copy the code

In mode 1, it returns a date1 object that holds the time when the object was created. In mode 2, you need to specify at least two parameters: year and month. The default values are automatically used when the following parameters are omitted. When using mode 3, specify at least a year. In addition, when the value passed in is greater than a reasonable range, it is automatically converted to adjacent values (for example, mode 2 sets the month to -1 for December last year and 12 for January next year).

After getting the date object, the output object is a string representing the date and time. If you want to represent the date and time in another format, you can do so by calling the date object’s related methods. Common methods for date objects fall into two categories: GET and set.

Get methods for Date objects:

methods role
getFullYear() Gets a 4-digit year, such as 2020
getMonth() Gets the month, ranging from 0 to 11(0 for January, 1 for February, and so on)
getDate() The value ranges from 1 to 31
getDay() Gets the week, ranging from 0 to 6(0 means Sunday, 1 means Monday, and so on)
getHours() The value ranges from 0 to 23 hours
getMinutes() Obtain the number of minutes. The value ranges from 0 to 59
getSeconds() Gets the number of seconds. The value ranges from 0 to 59
getMilliseconds() Gets the number of milliseconds, ranging from 0 to 999
getTime() Gets the number of milliseconds from 1970-01-01 00:00:00 to the time represented by the Date object

The set method for Date objects is as follows:

methods role
setFullYear(value) Set the date
setMonth(value) Set up in
setDate(value) Set a day in the month
setHours(value) Set hours
setMinutes(value) Set minutes
setSeconds(value) Set the number of seconds
setMillseconds(value) Set the number of boahs
setTime(value) The time is set by the number of milliseconds timed from 1970-01-01 00:00:00

Example: Output the current date in the console.

var date = new Date(a);// Create a Date object based on the current Date and time
var year = date.getFullYear();/ / for years
var month = date.getMonth();/ / for months
var day = date.getDate();/ / for days
// Convert the week value to a string through an array
var week =['Sunday'.'Monday'.'Tuesday'.'Wednesday'.'Thursday'.'Friday'.'Saturday'];
// Print the time the date object was saved, for example: Today is Wednesday 16 September 2019,
console.log('Today is'+ year + 'years' + month + 'month' + day + 'day' + week[date.getDay()]);
Copy the code

In the code above, week[date.getday ()] is used to get the week value from the date object, and then used as an index of the number to retrieve the corresponding week string from the week array.

Example: Convert the time in a date object to the specified format,

// Returns the current time in the format of hour: minute: second
function getTime(){

var time = new Date(a);var h = time.getHours();
h = h < 10 ? '0'+ h : h;
var m = time.getMinutes();
m = m < 10 ? '0' + m : m;
var s = time.getSeconds();
s = s<10?'0'+s:s;

return h + ':' + m + ':'+s;
}
console.log( getTime() ); // Example output: 10:07:56
Copy the code

In the above code, lines 5, 7, and 9 determine whether a given number is a single digit, and if so, prefix it with “0”.

5.4.2_[Case] Statistics the code execution time

The date object retrieves the number of milliseconds that have elapsed from 00:00:00:00, January 1, 1970 to the current UTC time. This value can be used as a timestamp. Time stamps can be used to calculate the time difference between two times, and can also be used in encryption, digital signature and other technologies.

The common ways to obtain the timestamp are as follows:

// Method 1: through the valueof() or getTime() methods of the date object
var datel = new Date(a);console.log( datel.valueOf() );
// Example result: 1571196996188
console.log( datel.getTime() );
// Example result: 1571196996188

// Method 2: use the "+" operator to convert to a numeric type
var date2 = +new Date(a);console.log(date2);
// Example result: 1571196996190

// Method 3: Use the date.now () method added to HIMLS
console.log( Date.now() );
// Example result: 1571196996190
Copy the code

After mastering how to obtain the timestamp, we will complete the code writing of the case, the specific code is as follows.

var timestamp1 = +new Date(a);for(var i = 1, str = ' '; i <= 90000; i++){
str += i;
}

var timestamp2 = +new Date(a);// Example result: Code execution time: 37 ms
console.log('Code execution time :' + (timestamp2 - timestamp1) + '毫秒');
Copy the code

As you can see from the above code and sample output, the JavaScript concatenated the string variable STR 90,000 times, taking a total of 37 milliseconds to execute. This time will vary depending on the speed of different computers.

5.4.3_[Case] Countdown

On the event page of some e-commerce websites, there will often be a countdown mark of discount goods, showing that there are X days X hours X minutes X seconds left before the end of the event. Such a countdown effect can be realized by using the date object.

The core algorithm of the countdown is the input time minus the present time, the remaining time is to display the countdown. This requires converting the time to a timestamp (milliseconds) for calculation, converting the resulting milliseconds to days, hours, minutes, and seconds.

The specific code is as follows:

function countDown(time){
var nowTime = +new Date(a);//+new Date() is short for the new Date().getTime() code, which returns the current timestamp in milliseconds.
var inputTime = +new Date(time);
// Set the end time stamp of the activity.
var times  = (inputTime - nowTime) / 1000;
// Calculate the number of remaining milliseconds, need to convert to seconds, conversion rule is 1 second /1000 milliseconds.
var d = parseInt(times / 60 / 60 / 24);
d = d < 10 ? '0' + d : d;
var h = parseInt (times / 60 / 60 % 24);
h = h < 10 ? '0' + h : h;
var m = parseInt(times / 60 % 60);
m = m < 10 ? '0' + m : m;
var s = parseInt(times % 60);
s = s < 10 ? '0' + s : s;
return d + 'day' + h + 'when' + m + 'points' + s + '秒';
// Lines 5 to 13 calculate days d, hours h, hours m, seconds s, and return with returm.
}
// Example result: 05 days 23:06:10 seconds
console.log(countDown('the 2019-10-22 10:56:57'));
// Output how much time remains before the specified end date.
Copy the code

That’s the end of today’s introductory study

Peace

🌊🌈

A Ken HTML, CSS introduction guide (a)_HTML basics a Ken HTML, CSS introduction guide (b)_HTML page elements and attributes a Ken HTML, CSS introduction guide (c)_ text style attributes Ken HTML, CSS introduction guide (four)_CSS3 selector Ken HTML, CSS introduction guide (five)_CSS box model Ken HTML, CSS introduction guide (six)_CSS box model Ken HTML, CSS introduction guide (seven)_CSS box model Ken’s Getting Started with HTML and CSS (8)_CSS box model Ken’s Getting Started with HTML and CSS (9)_ Floating and Positioning Ken’s Getting Started with HTML and CSS (10)_ Floating and Positioning Ken’s getting Started with HTML and CSS (11)_ Floating and positioning Ken’s introduction to HTML and CSS (12)_ The application of forms The introduction to HTML and CSS (13)_ the application of forms (14)_ the application of forms the introduction to HTML and CSS (15)_ the application of forms A Ken HTML, CSS introduction guide (16) _ multimedia technology

Suggest collection 】 【 HTML dry share | challenge the shortest possible time to take you into the HTML (18) suggest collection 】 【 HTML dry share | challenge the shortest time take you into the HTML (19) suggest collection 】 【 HTML dry share | challenge the shortest time take you into the HTML (20)

Suggest collection 】 【 share | JS dry challenge the shortest possible time to take you into the JS (a) suggest collection 】 【 share | JS dry challenge the shortest possible time to take you into the JS (2) suggest collection 】 【 share | JS dry challenge the shortest time to take you into the JS (3) the share | JS dry Recommended collection 】 challenge the shortest time take you into the JS (4) suggest collection 】 【 share | JS dry challenge the shortest time take you into the JS (5) suggest collection 】 【 share | JS dry challenge the shortest time take you into the JS (6) suggest collection 】 【 share | JS dry challenge the shortest possible time to take you into the JS (7) [JS dry share | suggestion collection] challenges the shortest time take you into the JS (8)

🌊🌈 About postscript:

Thank you for reading, I hope to help you if there is a flaw in the blog, please leave a message in the comment area or add contact information in the home page of the personal introduction of private chat I thank every little partner generous advice

Original is not easy, “like” + “attention” thanks for your support ❤