Js various time conversion problems (YYYY-MM-DD timestamp Chinese standard time)

1. Type summary

  1. Specify the format YYYY-MM-DD HH:MM:SS
  2. timestamp
  3. China Standard Time Sat Jan 30 2022 08:26:26 GMT+0800 (China Standard Time) new Date()to obtain the current time of the system will be in this form

2. Conversion between types

  1. Timestamp converted to yyyy-mm-dd or yyyy-MM-dd HH-mm-ss
function timestampToTime(timestamp) {
    
    
        var date = new Date(timestamp * 1000);//时间戳为10位需*1000,时间戳为13位的话不需乘1000
        var Y = date.getFullYear() + '-';
        var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1):date.getMonth()+1) + '-';
        var D = (date.getDate()< 10 ? '0'+date.getDate():date.getDate())+ ' ';
        var h = (date.getHours() < 10 ? '0'+date.getHours():date.getHours())+ ':';
        var m = (date.getMinutes() < 10 ? '0'+date.getMinutes():date.getMinutes()) + ':';
        var s = date.getSeconds() < 10 ? '0'+date.getSeconds():date.getSeconds();
        return Y+M+D+h+m+s;
    }
  1. yyyy-mm-dd or yyyy-MM-dd HH-mm-ss to timestamp
var stringTime = '2012-10-12 22:37:33';
//将获取到的时间转换成时间戳
var timestamp = Date.parse(new Date(stringTime));
  1. China Standard Time to yyyy-mm-dd hh-mm-ss
 let y = date.getFullYear()
 let m = date.getMonth() + 1
 m = m < 10 ? ('0' + m) : m
 let d = date.getDate()
 d = d < 10 ? ('0' + d) : d
 let h =date.getHours()
 h = h < 10 ? ('0' + h) : h
 let M =date.getMinutes()
 M = M < 10 ? ('0' + M) : M
 let s =date.getSeconds()
 s = s < 10 ? ('0' + s) : s
 let dateTime= y + '-' + m + '-' + d + ' ' + h + ':' + M + ':' + s;
  1. yyyy-mm-dd hh-mm-ss to China Standard Time
    1. new Date("month dd,yyyy hh:mm:ss");
    2. new Date("month dd,yyyy");
    3. new Date (yyyy,mth,dd,hh,mm,ss); Note: In this way, integer must be passed;
    4. new Date(yyyy,mth,dd);
    5. new Date(ms); Note: ms: It is the number of milliseconds between the time that needs to be created and GMT time January 1, 1970; the number of milliseconds between the current time and GMT1970.1.1: var mills = new Date().getTime();

  2. Timestamp converted to China Standard Time

const time = 1531065600000//时间戳(数字)
const youData = new Data(time);
  1. Convert China Standard Time to Timestamp
Date.parse(Time)

3. Date type

Create date object let now = new Date();
insert image description here

When no parameters are passed to the Date constructor, the created object will hold the current date and time. To create a date object based on other dates and times, you need to pass in the millisecond representation.

Types: Date.parse() && Date.UTC() && Date.now() && Date.toLocaleString() && Date.toString()


Parameter types supported by Date.parse()
: 1) month/day/year eg: '1/18/2023'
insert image description here
2) month name day, year eg: 'May 23, 2019'
insert image description here
3) week month name day year time : Minutes: Seconds Time zone eg: 'Wed Jan 18 2023 16:21:53 GMT+0800'
insert image description here
4) YYYY-MM-DDTHH:mm:ss.sssZ eg: 2019-05-23T00:00:00
insert image description here
If the parameter passed in Does not represent a date, returns NaN

Usage :

insert image description here
Date.UTC()
0:00, January 1, 2000,
insert image description here
5:55:55 pm, May 5, 2005 (note that the month starts from 0)
insert image description here

Date.now() current time
insert image description here

Date.toLocaleString() && Date.toString()

insert image description here

4. Date formatting

toDateString()
insert image description here

toTimeString()
insert image description here

toLocaleDateString()
insert image description here

toLocaleTimeString()
insert image description here

toUTCString()
insert image description here

5. How to judge whether it is the time of the day

if (new Date(str).toDateString() === new Date().toDateString()) {
    
    
    //今天
    console.log("当天");
} else if (new Date(str) < new Date()){
    
    
    //之前
    console.log(new Date(str).toISOString().slice(0,10));
}

Guess you like

Origin blog.csdn.net/qq_41867900/article/details/126610559