30

Let's say I type the following code in the console:

var TheDate = new Date(2012, 10, 5);
TheDate.toUTCString();
"Sun, 04 Nov 2012 23:00:00 GMT" (I'm +1 hour ahead of GMT)

The result is that the date is actually set to the local time. How do I create a Date that's set to UTC? If I do TheDate.toUTCString() I want it to say 05 Nov 2012 00:00:00 GMT.

Thanks.

4 Answers 4

63

Use the Date.UTC() method:

var TheDate = new Date( Date.UTC(2012, 10, 5) );
console.log( TheDate.toUTCString() );

returns

Mon, 05 Nov 2012 00:00:00 GMT

Date.UTC

Accepts the same parameters as the longest form of the constructor, and returns the number of milliseconds in a Date object since January 1, 1970, 00:00:00, universal time.

Sign up to request clarification or add additional context in comments.

4 Comments

What is the functional difference between new Date( Date.UTC(2012, 10, 5) ); and Date.UTC(2012, 10, 5); ?
@JakeT. Date.UTC() just returns the timestamp (a number). new Date( Date.UTC() ) will return a Date object.
I run TheDate.getTimezoneOffset();. I don't get 0 as I would expect, any idea how to resolve this?
@JoshBowling—a Date object doesn't have an offset, they're UTC. getTimezoneOffset returns the offset for the host system for the moment in time represented by the Date. Change system settings to a timezone with a different offset and getTimezoneOffset will return a different value.
3

I found a shorthand for it, You could also create your date in ISO format as new Date('YYYY-MM-DD') to create date as UTC:

var DateA = new Date( '2012-11-05' );
console.log( DateA.toUTCString() );

// note the difference between input methods

var DateB = new Date( Date.UTC(2012, 10, 5) );
console.log( DateB.toUTCString() );

Comments

0

I ran into the same problem and found a few more ways in addition to the ones already written here.

Way №1

Let's say I type the following code in the console:
var TheDate = new Date(2012, 10, 5);

If you already have ready-made time numbers/string, for example [2012, 10, 5] or from <input type="datetime-local">, and you want to keep them as they are (as if they were UTC), then you can create string in Date time string format and use it:

let foo = new Date("2012-11-05T00:00:00Z")
console.log(foo.toUTCString()) // "Mon, 05 Nov 2012 00:00:00 GMT"
// or
let bar = new Date("2012-11-05T00:00:00+00:00")
console.log(bar.toUTCString()) // "Mon, 05 Nov 2012 00:00:00 GMT"

Attention: When the time zone offset is absent, date-only forms are interpreted as a UTC time and date-time forms are interpreted as a local time.

P.S.: If you only need a timestamp, you can get it using Date.parse():

let ts = Date.parse("2012-11-05T00:00:00Z")
// or
//let ts = Date.parse("2012-11-05T00:00:00+00:00")
console.log(ts) // 1352073600000
console.log(new Date(ts).toUTCString()) // "Mon, 05 Nov 2012 00:00:00 GMT"

Way №2

After creating a Date object, you can change its inner time using an offset to the environment timezone, this will save the time like it's in UTC.

let d = new Date(2012, 10, 5);
console.log(d.toUTCString()); // "Sun, 04 Nov 2012 23:00:00 GMT"
d.setTime( d.getTime() - d.getTimezoneOffset() * 60 * 1000 );
// 1352059200000 - 60(minutes) * 60(seconds) * 1000(milliseconds)
console.log(d.toUTCString()); // "Mon, 05 Nov 2012 00:00:00 GMT"

Source: Matt Johnson-Pint answer to the question "get UTC date (not UTC string) in javascript"

Comments

-5

I would suggest you use momentjs (

momentjs.com

), then all you have to do is:

var theDate = new Date(2012, 10, 5),
    utcDate = moment.utc(theDate);

1 Comment

This doesn't answer the question. The author wants a Date object, moment.utc does not return a Date.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.