Fatmetal
Fatmetal

Reputation: 5

Is there a JS alternative to Date.now() that doesn't change according to timezones?

I built a script to put versions on some data my website fetches, but since users from all over the world use it Date.now() returns different results according to the timezone the user is in.

Is there an alternative to Date.now() or an option I overlooked that can standardize this?

Edit: This is what I need the date for:

function timeDB(){
    let unix_timestamp = Date.now();
    var date = new Date(unix_timestamp);
    var hours = date.getHours();
    var year =  date.getFullYear();
    var month =  date.getMonth()+1;
    var day = date.getDate();

if ( month <10){
    year = year*10;
}
if ( day <10){
    month = month*10;
}
if ( hours <10){
    day = day*10;
}
    var formattedTime = year + '' + month + '' + day + '' + hours;
    return formattedTime;
}

This formated time will then be used as a version in my DB, so I need a universal timestamp.

Upvotes: 0

Views: 2329

Answers (3)

RobG
RobG

Reputation: 147343

Date.now returns an offset in milliseconds from 1970-01-01 UTC, it's unaffected by timezones or daylight saving, however it is affected by the accuracy of the host system's clock.

For a "universal" timestamp, you should use Universal Coordinated Time (UTC) values. You can build your own string using getUTC* methods such as getUTCFullYear, getUTCMonth, etc. or just use the string returned by toISOString and reformat the parts to suit.

If you just want YYYY MM DD HH then you can use a very simple function, the values are already zero padded strings so no need for the * 10 trick:

function timeDB(date = new Date()) {
  return date.toISOString().split(/\D/).splice(0,4).join('');
}

console.log(timeDB());

There are many questions and answers already on how to format a date.

However, if you just want a unique value for versioning and only using current dates, then just using the value returned by Date.now should be sufficient. It's only 3 digits longer and you can trim it to 10 digits if you wish:

String(Date.now()).slice(0,10);

That effectively gives you a timestamp in seconds since the (fairly common) epoch of 1970-01-01 and is easily turned back into a date.

Upvotes: 2

Ahmed Abdelfattah
Ahmed Abdelfattah

Reputation: 577

I think you need UTC version of date and time so your code should be

function timeDB(){
    let unix_timestamp = Date.now();
    var date = new Date(unix_timestamp);
    var hours = date.getUTCHours();
    var year =  date.getUTCFullYear();
    var month =  date.getUTCMonth()+1;
    var day = date.getUTCDate();

if ( month <10){
    year = year*10;
}
if ( day <10){
    month = month*10;
}
if ( hours <10){
    day = day*10;
}
    var formattedTime = year + '' + month + '' + day + '' + hours;
    return formattedTime;
}

This is will return the same values regardless the country is served from.

Upvotes: 0

slebetman
slebetman

Reputation: 113876

If your environment is properly configured to know its timezone (most browsers are) then all you need to do is add the timezone offset to your timestamp:

Date.now() + new Date().getTimezoneOffset() * 60 * 1000

Upvotes: 1

Related Questions