Js between different time formats and describes the conversion

First must be mentioned is the Date object, which is used to deal with the time and date.

Using new Date () statement creates a Date object created out time format (standard time mentioned later refer to this format):

Wed Jul 17 2019 13:59:21 GMT + 0800 (China Standard Time)

Date objects There are several ways to create:

1. let date = new Date();
2. let date = new Date(milliseconds);
3. let date = new Date(dateString);
4. let date = new Date(year, month, day, hours, minutes, seconds);

Explanation:

1. The reference does not pass, the current time is acquired;

2. pass the time stamp , the time stamp refers GMT January 1, 1970 00 hours 00 minutes 00 seconds (Beijing time on January 1, 1970 08 hours 00 minutes 00 seconds) until now the total number of seconds, a 10 digits (ms is 13), where 10 mass 13 may be;

3. The transmission time of a string, such as "2019/7/17 14:40:30 ' or ' 2019-7-17 ' , where I tried with' / 'and' - 'for the date separator are possible, space and not a comma, date to complete, minutes and seconds can not be written in full;

4. pass a comma-separated time, such as 2019,7, 17, 14, 40, 30 , I tried it years this is a must, the other can not write the whole. Note, month on behalf of the month is from 0 (January) to 11 (December).

 

Conversion timestamp and date formats:

 

First, the time stamp is converted to standard time format

As already mentioned, the timestamp as an argument directly to the new Date () can

 

let date = new Date(timestamp)

 

Second, the time stamp is converted to standard time format

This is also very simple, use the Date object's getTime () method:

let timestamp = new Date().getTime()

13 is a time stamp is obtained here, i.e. milliseconds

 

Third, the standard time format is converted into the specified format

Some methods can take advantage of a Date object

 

Date.getFullYear ()  // for a complete year 

date.getMonth () // get the month (0-11,0 behalf January, when used together remember 1) 

date.getDate () // Gets Day (1- 31 is) 

date.getTime () // Get the time (number of milliseconds from the start 1970.1.1) 

date.getHours () // Get the number (0-23) hours 

date.getMinutes () // Get the number of minutes (0-59 ) 

date.getSeconds () // get the number of seconds (0-59)

 

What time format required to obtain a desired portion and then spliced ​​together by the above method.
 
Fourth, the timestamp converted to the specified format
First time stamp is converted to standard time format, press three converted to the specified format.
 
Added: When we want to deal with time, such as plus one day a day cut, first into a timestamp and then calculated. To obtain an hour before the current time, may be calculated as follows:
new Date(new Date().getTime() - 1*60*60*1000)
 
Reference links:
https://www.runoob.com/jsref/jsref-obj-date.html
 
https://www.jb51.net/article/130303.htm

 

Guess you like

Origin www.cnblogs.com/zdd2017/p/11202109.html