Posted By: Anonymous
Suppose a user of your website enters a date range.
2009-1-1 to 2009-1-3
You need to send this date to a server for some processing, but the server expects all dates and times to be in UTC.
Now suppose the user is in Alaska or Hawaii or Fiji. Since they are in a timezone quite different from UTC, the date range needs to be converted to something like this:
2009-1-1T8:00:00 to 2009-1-4T7:59:59
toISOString()method returns a string in simplified extended ISO
format (ISO 8601), which is always 24 or 27 characters long
respectively). The timezone is always zero UTC offset, as denoted by
the suffix "
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);