1
votes

I know there are various ways using which we can convert date time into Unix timestamp.

But the problem is when we convert GMT's Date time value into Unix timestamp then it shows the value of timestamp according to my local timezone's value i.e. Asia/Kolkata

For example: Date time of GMT is: 2018-08-21 11:37:56 So when I am converting it into Unix timestamp then it returns as follows:

var t = new Date('2018-08-21 11:37:56').getTime() / 1000;
console.log(t); // 1534831676

I am looking for 1534851476 which is a value according to the GMT time zone.

which is Unix timestamp according to my local time zone Asia/Kolkata. Can anyone help me out to get Unix timestamp value into GMT?

For more information: you can check the Unix timestamp value of GMT date time here on this link.

Thank you!

1
try new Date('2018-08-21T11:37:56Z').getTime() / 1000Jaromanda X
If you don't include time zone information in the date string, how can you expect it to know that you want UTC time?Pointy
on that point, some variants of input to Date constructor do assume UTC :pJaromanda X
@JaromandaX I am having value of date time as 2018-08-21 11:37:56 in the database.Deep Kakkar
well that's simple to change ... '2018-08-21 11:37:56'.replace(' ', 'T')+'Z'Jaromanda X

1 Answers

2
votes

Without a timezone, new Date assumes localtime

so, lets say you retrieve the string

var date = '2018-08-21 11:37:56';

what you can do is

var date = '2018-08-21 11:37:56';
var t = new Date(date.replace(' ', 'T') + 'Z').getTime() / 1000;
console.log(t); // 1534851476 - see for yourself