Compare date and timestamp with timezone

Viewed 89

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")
}
1 Answers

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))

Related