How to Get a Utc Timestamp in JavaScript

How do I get a UTC Timestamp in JavaScript?

  1. Dates constructed that way use the local timezone, making the constructed date incorrect. To set the timezone of a certain date object is to construct it from a date string that includes the timezone. (I had problems getting that to work in an older Android browser.)

  2. Note that getTime() returns milliseconds, not plain seconds.

For a UTC/Unix timestamp, the following should suffice:

Math.floor((new Date()).getTime() / 1000)

It will factor the current timezone offset into the result. For a string representation, David Ellis' answer works.

To clarify:

new Date(Y, M, D, h, m, s)

That input is treated as local time. If UTC time is passed in, the results will differ. Observe (I'm in GMT +02:00 right now, and it's 07:50):

> var d1 = new Date();
> d1.toUTCString();
"Sun, 18 Mar 2012 05:50:34 GMT" // two hours less than my local time
> Math.floor(d1.getTime()/ 1000)
1332049834

> var d2 = new Date( d1.getUTCFullYear(), d1.getUTCMonth(), d1.getUTCDate(), d1.getUTCHours(), d1.getUTCMinutes(), d1.getUTCSeconds() );
> d2.toUTCString();
"Sun, 18 Mar 2012 03:50:34 GMT" // four hours less than my local time, and two hours less than the original time - because my GMT+2 input was interpreted as GMT+0!
> Math.floor(d2.getTime()/ 1000)
1332042634

Also note that getUTCDate() cannot be substituted for getUTCDay(). This is because getUTCDate() returns the day of the month; whereas, getUTCDay() returns the day of the week.

How get timestamp by UTC Date in javascript?

Use Date.UTC() instead of new Date():

var mill= Date.UTC(2017,06,16,12,0,7);

alert(mill);

new Date() assumes the values provided are for the browser's current timezone

How to convert a Date to UTC?

The toISOString() method returns a string in simplified extended ISO
format (ISO 8601), which is always 24 or 27 characters long
(YYYY-MM-DDTHH:mm:ss.sssZ or ±YYYYYY-MM-DDTHH:mm:ss.sssZ,
respectively). The timezone is always zero UTC offset, as denoted by
the suffix "Z".

Source: MDN web docs

The format you need is created with the .toISOString() method. For older browsers (ie8 and under), which don't natively support this method, the shim can be found here:

This will give you the ability to do what you need:

var isoDateString = new Date().toISOString();
console.log(isoDateString);

Create UTC Timestamp in Javascript

You can use date.toJSON().

new Date().toJSON()
"2013-08-31T09:05:07.740Z"

See MDN or MSDN

How to get a timestamp in UTC but without the time?

I don't know what you mean by long but the code for extracting only the part you're looking for is pretty short.

'Wed, 19 Jan 2021 19:27:00 GMT'.split(' ').slice(0, -2)

Edit: If you really dislike this code I just found out that Date.toDateString() exists

How do I get a timestamp in JavaScript?

Timestamp in milliseconds

To get the number of milliseconds since Unix epoch, call Date.now:

Date.now()

Alternatively, use the unary operator + to call Date.prototype.valueOf:

+ new Date()

Alternatively, call valueOf directly:

new Date().valueOf()

To support IE8 and earlier (see compatibility table), create a shim for Date.now:

if (!Date.now) {
Date.now = function() { return new Date().getTime(); }
}

Alternatively, call getTime directly:

new Date().getTime()


Timestamp in seconds

To get the number of seconds since Unix epoch, i.e. Unix timestamp:

Math.floor(Date.now() / 1000)

Alternatively, using bitwise-or to floor is slightly faster, but also less readable and may break in the future (see explanations 1, 2):

Date.now() / 1000 | 0


Timestamp in milliseconds (higher resolution)

Use performance.now:

var isPerformanceSupported = (
window.performance &&
window.performance.now &&
window.performance.timing &&
window.performance.timing.navigationStart
);

var timeStampInMs = (
isPerformanceSupported ?
window.performance.now() +
window.performance.timing.navigationStart :
Date.now()
);

console.log(timeStampInMs, Date.now());

get UTC timestamp from formated date - javascript

You can decrease the "TimezoneOffset"

var d = new Date("10/30/2014 10:37:54 AM");
return d.getTime() - (d.getTimezoneOffset() *1000 * 60);

Also u can use the UTC function

var d = new Date("10/30/2014 10:37:54 AM");
return Date.UTC(d.getFullYear(),d.getMonth()+1,d.getDate(),d.getHours(),d.getMinutes(),d.getSeconds());

Retrieve UTC timestamp with Javascript

The way you are doing it works and should be reliable. If I had written it I would have done it a little differently.

var start = new Date(),
end;

// set midnight today
start.setUTCHours(0);
start.setUTCMinutes(0);
start.setUTCSeconds(0);
start.setUTCMilliseconds(0);

// set to midnight tomorrow
end = new Date(start);
end.setUTCDate(start.getUTCDate() + 1);

console.log("start of the day: " + start.getTime());
console.log("end of the day: " + end.getTime());

This code creates the start date as the current date/time and then zeros out the time portions. Then getting 24 hours later is just a matter of creating a duplicate Date and incrementing the date by 1. It does require a little more code but it makes it explicit what you are doing and will probably be easier to read and comprehend when maintaining the code in the future.



Related Topics



Leave a reply



Submit