js time object

Posted by cytech on Sun, 09 Jan 2022 14:40:16 +0100

1, What is a time object

Store recording time data

2, Time object creation syntax

Create time object through constructor

There are no parameters to create a time object of the current time by default
var variable = new Date();

Set parameters to create a time object at a specified time
var variable = new date (parameter);

//Time object for current time
var time = new Date();
console.log(time);

//Specifies the time object for the time
//The number must match the time value range

//Parameter syntax form 1
//Parameter syntax year month day hour: minute: Second
var time2 = new Date('2022-1-9 19:53:34');
console.log(time2);

//Parameter syntax year month day hour: minute: Second
var time3 = new Date('2022 1 9 19:53:34');
console.log(time3);

//Parameter syntax year / month / day hour: minute: Second
var time3 = new Date('2022/1/9 19:53:34');
console.log(time3);

//Parameter syntax year month day hour: minute: Second
var time4 = new Date('2022,1,9 19:53:34');
console.log(time4);

//Parameter syntax form 2 6 numbers
//The number set for the month is 0 - 11, corresponding to 1 - 12 months
//The number format can be outside the normal range 
//It will automatically carry forward one time unit

var time5 = new Date(2022,0,9,19,53,34);
console.log(time5);

//The set month is 15, corresponding to 1 year and 4 months
//Enter January into the year unit and display April
var time6 = new Date(2022,15,9,19,53,34);
console.log(time6);

Execution results:

3, Function method of time object

Gets the time object get... ()

Obtain the specific time data stored in the time object through the function method provided by the JavaScript program

1. Time object getFullYear()

Get four digit year

2. Time object getMonth()

Get month

The result obtained is a number from 0 to 11

It corresponds to the months from January to December

3. Time object get Date()

Get date

4. Time object getDay()

Get week

The result obtained is a number from 0 to 6 +

It corresponds to Sunday ~ Saturday

5. Time object getHours()

Get hours

6. Time object getMinutes()

Get minutes

7. Time object getSeconds()

Get seconds

8. Case: get the current time

//Create time object
var currentTime = new Date();

//Output data to console
console.log(currentTime);
//Output attribute values to the console
console.dir(currentTime);

//Get the specific time data in the time object

//Get 4-digit year
var year = currentTime.getFullYear();
console.log(year);

//Get month
//Get 0 ~ 11, perform + 1 operation and output 1 ~ 12
var month = currentTime.getMonth() + 1 ? '0' + (currentTime.getMonth() + 1) : currentTime.getMonth() + 1;
console.log(month);

//get date
var day = currentTime.getDate() < 10 ? '0' + currentTime.getDate() : currentTime.getDate();

//Get week
//Get results 0 ~ 6
//You can set an array of weeks
var weekArr = [ 'Sunday' , 'Monday' , ' Tuesday' , 'Wednesday' , 'Thursday' , 'Friday' , 'Saturday' ] ;
var week = weekArr[currentTime.getDay()];
console.log(week);

//Get hours
//The local time of the corresponding hour is obtained
var hour = currentTime.getHours() < 10 ? '0' + currentTime.getHours() : currentTime.getHours();
console.log(hour);

//Get the corresponding hour world standard time
//var hour = currentTime.getUTCHours();

//Get minutes
var minute = currentTime.getMinutes() < 10 ? '0' + currentTime.getMinutes() : currentTime.getMinutes();
console.log(minute);

//Get seconds
var seconds = currentTime.getSeconds() < 10 ? '0' + currentTime.getSeconds() : currentTime.getSeconds();
console.log(seconds);

Execution results:

IV. time stamp

1. Understanding of timestamp:

Time difference from 0:00:00:00 on January 1, 1970

The unit of timestamp in JavaScript is milliseconds

1 second = 1000 milliseconds

2. Purpose of timestamp:

Time stamps are generally used to calculate the time difference

Then, the time difference is converted into the corresponding day, hour, minute and second

3. Get timestamp

//Create time object
var time = newDate();

//Get timestamp
var td = time.getTime();

// Leading zeroing before converting to days, hours, minutes and seconds
// day
var day = parseInt(td / (24*60*60));

//hour
var hour = parseInt(td % (24*60*60) / (60*60));

//minute
var minute = parseInt(td % (60*60) / 60);

//second
var seconds = td % 60;

4. Calculate the current time from the Spring Festival: the countdown to 00:00 on February 1, 2022

4.1 create time object

Start time --- current time

End time --- set time

4.2 calculation time difference

Timestamp of end time - timestamp of start time

Convert to seconds

4.3} convert the time difference (seconds) into days, hours, minutes and seconds

//Creates a time object for the current time
var startTime = new Date();
var endTime = new Date(2022,2,1,0,0,0);

//Calculate time difference
var td = parseInt((endTime.getTime() - startTime.getTime()) / 1000);

//Leading zeroing before converting to days, hours, minutes and seconds

// day
var day = parseInt( td / (24*60*60) );

// hour 
var hour = parseInt( td % (24*60*60) / (60*60) );

// minute
var minute = parseInt( td % (60*60) / 60 );

// second
var seconds = td % 60 ;

//Page output
document.write(`There is still room for the Spring Festival ${day < 10 ? '0' + day : day}day ${hour < 10 ? '0' + hour : hour}hour ${minute < 10 ? '0' + minute : minute}minute ${seconds < 10 ? '0' + seconds : seconds}second`);

Execution results:

5, Timer delay

1. Timer

Execute the program at the set time interval

The unit of time interval is milliseconds

That is, the program is repeated at intervals

Grammar

Setinterval (parameter 1, parameter 2);

Parameter 1 # function program executed

Valuepoint 2} time interval

//timer
//The program defined in the anonymous function is executed every 3000 milliseconds, that is, every 3 seconds

//Grammatical form 1
setInterval(function() {console.log('I'm trying to blog')},3000);

//Grammatical form 2
setInterval(fn,3000);
function fn() {
    console.log('I'm trying to blog')
}

2. Time delayer

Execute the program once according to the interval delay

That is to delay the execution of the program according to the set time interval

grammar

SetTimeout (parameter 1, parameter 2);

Parameter 1 # function program executed

Valuepoint 2} time interval

//Delay device
//After an interval of 3 seconds, it will only be triggered once

//Grammatical form 1
setTimeout(function() {console.log('I'm trying to blog')},3000);

//Grammatical form 2
setTimeout(fn,3000);
function fn() {
   console.log('I'm trying to blog');  
}

6, Clear timer delay

grammar

Clearinterval (parameter)

Cleartimeout (parameter)

Both functions can clear both the timer and the delayer

The parameter is the serial number of timer or delayer

// The execution effect of timer delayer is to trigger the function program set by parameter 1 according to the interval time
// The return value of their execution result is the return value defined internally by the timer delayer function
// This means that the variable stores the data value returned by return in the timer delayer function 
// That is, the serial number of the timer delayer
var num1 = setTimeout( function(){console.log(111)} , 1000 );
var num2 = setTimeout( function(){console.log(222)} , 2000 );
var num3 = setTimeout( function(){console.log(333)} , 3000 );
var num4 = setTimeout( function(){console.log(444)} , 4000 );

var num5 = setInterval( function(){console.log(555)} , 1000 );

console.log(num1); // Execution result 1
console.log(num2); // Implementation result 2
console.log(num3); // Implementation result 3
console.log(num4); // Implementation result 4
console.log(num5); // Implementation result 5

// The cleared parameter is the serial number of the timer delayer 
// You can define numbers directly, but we usually don't
//Because there are many lines of code in the actual project, it is difficult for us to calculate the serial number manually 
clearInterval(1);
// Variables can be used to store the return value of the execution result of the timer delayer
clearInterval(num2);

Topics: Javascript linq