UTC Times in JavaScript

I am trying to get the current UTC date to store in my database. My local time is 9:11 p.m. This equates to 1:11 a.m. UTC. When I look in my database, I notice that 1:11 p.m. is getting written to. I'm confused. In order to get the UTC time in JavaScript, I'm using the following code:

var currentDate = new Date();
var utcDate = Date.UTC(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate(), currentDate.getHours(), currentDate.getMinutes(), currentDate.getSeconds(), currentDate.getMilliseconds());
var result = new Date(utcDate);

What am I doing wrong?


A lttle searching turned out you can do this:

var now = new Date(),
    utcDate = new Date(
        now.getUTCFullYear(),
        now.getUTCMonth(),
        now.getUTCDate(),
        now.getUTCHours(),
        now.getUTCMinutes(), 
        now.getUTCSeconds()
    );

Even shorter:

var utcDate = new Date(new Date().toUTCString().substr(0, 25));

How do you convert a JavaScript date to UTC?

It is a commonly used way, instead of creating a ISO8601 string, to get date and time of UTC out. Because if you use a string, then you'll not be able to use every single native methods of Date(), and some people might use regex for that, which is slower than native ways.

But if you are storing it in some kind of database like localstorage, a ISO8601 string is recommended because it can also save timezone offsets, but in your case every date is turned into UTC, so timezone really does not matter.

The UTC() method differs from the Date constructor in two ways: Date.UTC() uses universal time instead of the local time. Date.UTC() returns a time value as a number instead of creating a Date object. If a parameter is outside of the expected range, the UTC() method updates the other parameters to


If you want the UTC time of a local date object, use the UTC methods to get it. All javascript date objects are local dates.

var date = new Date(); // date object in local timezone

If you want the UTC time, you can try the implementation dependent toUTCString method:

var UTCstring = date.toUTCString();

but I wouldn't trust that. If you want an ISO8601 string (which most databases want) in UTC time then:

var isoDate = date.getUTCFullYear() + '-' +
              addZ((date.getUTCMonth()) + 1) + '-' +
              addZ(date.getUTCDate()) + 'T' +
              addZ(date.getUTCHours()) + ':' +
              addZ(date.getUTCMinutes()) + ':' +
              addZ(date.getUTCSeconds()) + 'Z';

where the addZ function is:

function addZ(n) {
  return (n<10? '0' : '') + n;
}

Modify to suit.

Edit

To adjust a local date object to display the same time as UTC, just add the timezone offset:

function adjustToUTC(d) {
  d.setMinutes(d.getMinutes() + d.getTimezoneOffset()); 
  return d;
}

alert(adjustToUTC(new Date())); // shows UTC time but will display local offset

Take care with the above. If you are say UTC+5hrs, then it will return a date object 5 hours earlier but still show "UTC+5"

A function to convert a UTC ISO8601 string to a local date object:

function fromUTCISOString(s) {
  var b = s.split(/[-T:\.Z]/i);
  var n= new Date(Date.UTC(b[0],b[1]-1,b[2],b[3],b[4],b[5]));
  return n;
}

alert(fromUTCISOString('2012-05-21T14:32:12Z'));  // local time displayed

The getUTCDate() method returns the day of the month (from 1 to 31) of the date object, according to universal time. The UTC methods calculate their date assuming that the date object is of local time and date. Tip: The Universal Coordinated Time (UTC) is the time set by the World Time Standard. Note: UTC time is the same as GMT time.


var now = new Date();
var utc = new Date(now.getTime() + now.getTimezoneOffset() * 60000);

If you want the UTC time of a local date object, use the UTC methods to get it. All javascript date objects are local dates. var date = new Date(); // date object in local timezone If you want the UTC time, you can try the implementation dependent toUTCString method: var UTCstring = date.toUTCString(); but I wouldn't trust that. If you want an ISO8601 string (which most databases want) in UTC time then:


This method would be helpful in the frontend when you would want your user to show UTC date in a more readable format. Now, what if you want the current time in UTC format. Well, thanks to


34. GMT+0000 (UTC) The date and time is broken up and printed in a way that we can understand as humans. JavaScript, however, understands the date based on a timestamp derived from Unix time, which is a value consisting of the number of milliseconds that have passed since midnight on January 1st, 1970.


UTC / GMT is the basis for local times worldwide. Other names: Universal Time Coordinated / Universal Coordinated Time. Successor to: Greenwich Mean Time (GMT) Military name: “Zulu” Military Time. Longitude: 0° (Prime Meridian) At sea: Longitudes between 7.5° West and 7.5° East. Note: United Kingdom is one hour ahead of UTC / GMT during