Js-date and time stamp conversion, time comparison (without plug-in)

Small tips:

The meaning of new Date() is used to format data, and then we can use the various methods provided by js to look at the following content in this way

Date conversion -> timestamp

Current time to time stamp: let a = new Date().getTime()
Target time to time stamp:
let a = new Date('2021-08-31 13:05:05').getTime()
let a = new Date('2021/08/31 13:05:05').getTime()

Insert picture description here
getTime() is at the millisecond level. Date.parse() is on the second level.后者写法麻烦,不建议了
Insert picture description here

Timestamp to -> date

let a = new Date(1630386359000)
year: a.getFullYear()
month: a.getMonth()+1
day: a.getDate()
hour: a.getHours()
minute: a.getMinutes()
second: a.getSeconds () //All without 0
~~~~~~~~~~~ 一键复制即用的代码在文末 ~~~~~~~~~~~~~~~~~~~~~

Time comparison and judgment

The comparison is definitely the most stable comparison with the timestamp, and it is better to transfer the timestamp to the
7-day timestamp: For 1000*60*60*24*7
example, when it is greater than 7 days, it will be displayed in red. At this time, it is easier to judge.

Get the current date quickly (mostly used to export the time of the file)

let a = new Date().toLocaleDateString() —— 2021/3/8 (without adding 0)

Timestamp to date code

//传入时间戳
		gettime(val) {
    
    
            let date=new Date(val)
            let y=date.getFullYear()
            let m=date.getMonth()+1
            let d=date.getDate()
            let h=date.getHours() 
            let mu=date.getMinutes()
            let s=date.getSeconds()
            let time= y+'-'+m+'-'+d+' '+ this.add0(h) +':'+this.add0(mu)+':'+this.add0(s)
            return time
        },
        add0(m) {
    
    
            return m<10?'0'+m:m     //补0
        },
console.log(this.gettime(1630386359000))       //2021-8-31 13:05:59

Guess you like

Origin blog.csdn.net/weixin_45629623/article/details/114543286