简体   繁体   中英

Compare date and timestamp with timezone

I am using JavaScript and I have a date and a timestamp wit time zone and I would like to see if they are equal in a if statement. How do you compare these two formats to see if they are equal.

Date: 2018-12-25T06:00:00+01:00
Timestamp with timezone: 2018-12-25T11:00:00.000Z

var date = Date
var timestamp = Timestamp with timezone
if(date == timestamp){
  console.log("Are the same")
}

Pass both to new Date and cast to number using Date#getTime() or + operator then do an equality check.

The Z is for "Zulu" which is UTC time

 const d1 = '2018-12-25T06:00:00+01:00', d2 = '2018-12-25T05:00:00.000Z'; console.log(+new Date(d1) === +new Date(d2)) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM