Sam Leurs
Sam Leurs

Reputation: 490

UNIX timestamp of date on midnight UTC+0

I have the following date: 07/08/2018 in the format m/d/Y. This date is july 08, 2018.

And I have the following JavaScript code:

var dateFrom = '07/08/2018';
    dateFrom = new Date(dateFrom);
alert(dateFrom);

When I do this, I get the following result:

Sun Jul 08 2018 00:00:00 GMT+0200 (Midden-Europese zomertijd)

As you can see, because I live in Belgium, I get the time + GMT+2. But that's not what I want. I want the exact UNIX timestamp of 07/08/2018 (or any other date) of GMT+0.

I have the following JavaScript code:

var dateFrom = '07/08/2018';
    dateFrom = Math.floor((new Date(dateFrom)).getTime()/1000);
alert(dateFrom);

If I execute this code, I get the following result:

1531000800

But that's not what I want. If I check the UNIX timestamp I get (1531000800) on this (https://www.unixtimestamp.com/index.php) website, I get the following result:

1531000800
Is equivalent to:
07/07/2018 @ 10:00pm (UTC)

I want the UNIX timestamp that is equal to 07/08/2018 @ 00:00am (UTC).

How can I achieve this?

Thanks in advance!

Upvotes: 0

Views: 927

Answers (1)

Akrion
Akrion

Reputation: 18515

As per the MDM documentation:

The following statement creates a Date object using UTC instead of local time:

var utcDate = new Date(Date.UTC(2018, 11, 1, 0, 0, 0));

Code Sample:

var date = new Date()
var utcDate = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()))

console.log(date)
console.log(utcDate)

Upvotes: 3

Related Questions