get UTC timezone's timestamp in javascript

I am searching for a function that will return nearly same output for my all visitors which they are locating different countries

var d1 = new Date();
var d2 = new Date( d1.getUTCFullYear(), d1.getUTCMonth(), d1.getUTCDate(), d1.getUTCHours(), d1.getUTCMinutes(), d1.getUTCSeconds() );
Math.floor(d2.getTime()/ 1000);

I found this from stackoverflow but i see it doesn't work, it doesn't give same output with my vps which is located in france and totally different result from http://www.unixtimestamp.com/

var options = {
    timeZone: 'UTC',
    year: 'numeric', month: 'numeric', day: 'numeric',
    hour: 'numeric', minute: 'numeric', second: 'numeric'
},
formatter = new Intl.DateTimeFormat([], options)
formatter.format(new Date())

this returns me right result but it's not timestamp. Any help appreciated.

Upvotes: 0

Views: 82

Answers (2)

sorry it's my mistake javascript already uses utc as default ,

you can just use this code snipet to get utc timestamp

parseInt(new Date().getTime()/1000);

Upvotes: 0

Shanoor
Shanoor

Reputation: 13692

You need Date.UTC:

var d1 = new Date();
var d2 = Date.UTC(d1.getUTCFullYear(), d1.getUTCMonth(), d1.getUTCDate(), d1.getUTCHours(), d1.getUTCMinutes(), d1.getUTCSeconds());
var timestamp = Math.floor(d2 / 1000);
document.write(timestamp);

Upvotes: 1

Related Questions