4

I want to use the javascript's toISOString() function and ignore the timezone.

var someDate; // contains "Tue May 26 2015 14:00:00 GMT+0100 (Hora de Verão de GMT)"
dateIWant = someDate.toISOString(); // turns out "2015-05-26T13:00:00.000Z"

The date to convert is Tue May 26 2015 14:00:00 GMT+0100 (Hora de Verão de GMT) but the converted date is 2015-05-26T13:00:00.000Z.

So, I need the date in the yyyy-MM-ddTHH:mm:ss:msZ but as you can see above it applies the timezone and changes the hour from 14 to 13.

How to achieve this?

EDIT

I am working on a C# MVC project and I can send the date as it is and manipulate it in the C#. It is my current solution but I am looking for a client side one.

chiapa
  • 4,362
  • 11
  • 66
  • 106

2 Answers2

8

Based on the polyfill for Date.prototype.toISOString found at MDN's Date.prototye.toISOString:

if (!Date.prototype.toLocalISOString) {
  (function() {

    function pad(number) {
      if (number < 10) {
        return '0' + number;
      }
      return number;
    }

    Date.prototype.toLocalISOString = function() {
      return this.getFullYear() +
        '-' + pad(this.getMonth() + 1) +
        '-' + pad(this.getDate()) +
        'T' + pad(this.getHours()) +
        ':' + pad(this.getMinutes()) +
        ':' + pad(this.getSeconds()) +
        '.' + (this.getMilliseconds() / 1000).toFixed(3).slice(2, 5) +
        'Z';
    };

  }());
}

So just use this toLocalISOString instead of toISOString.

Ionut Costica
  • 1,382
  • 1
  • 9
  • 19
  • 1
    Thank you for your answer but there's no such method as `toLocalISOString` for a date. I don't kow what to do with that code – chiapa May 29 '15 at 13:50
  • There appears to be a typo in Ionut's method. I believe the one he meant was toLocaleISOString (note the "e" -- it's Locale, not Local). I know this is an old thread, but perhaps it will help someone else. :) – B.Rossow Dec 15 '17 at 14:41
0

Your Javascript statements are producing the expected results. Tue May 26 2015 14:00:00 GMT+0100 is the same time as 2015-05-26T13:00:00.000Z. Unless you care about a fraction of a second, GMT, UTC, and Z all mean the same thing: mean solar time at 0° longitude, and keeping the same time all year (no change in summer; Iceland observes this time all year). "GMT+0100" means a time zone that is one hour later than 0° longitude, such the United Kingdom in summer. So at a given moment the time in Iceland is 2015-05-26T13:00:00.000Z and also Tue May 26 2015 14:00:00 GMT+0100 in London. Could you clarify what result you want to see? Perhaps you would like "2015-05-26T14:00:00" which is the ISO 8601 notation for some unspecified local time, presumably the local time of the computer displaying the Javascript. Or maybe you want "2015-05-26T14:00:00+01:00", which is ISO 8601 notation for the local time zone one hour ahead of 0° longitude, such as London in summer or Paris in winter.

Building on souldreamer's example, making it fully working and providing the local time offset:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<body>
<pre>
<script language="JavaScript">
    if (!Date.prototype.toLocalISOString) {
        // Anonymous self-invoking function
        (function () {

            function pad(number) {
                if (number < 10) {
                    return '0' + number;
                }
                return number;
            }

            Date.prototype.toLocalISOString = function () {
                timestamp = this.getFullYear() +
                  '-' + pad(this.getMonth() + 1) +
                  '-' + pad(this.getDate()) +
                  'T' + pad(this.getHours()) +
                  ':' + pad(this.getMinutes()) +
                  ':' + pad(this.getSeconds());
                if (this.getTimezoneOffset() == 0) { timestamp = timestamp + "Z" }
                else {
                    if (this.getTimezoneOffset() < 0) { timestamp = timestamp + "+" }
                    else { timestamp = timestamp + "-" }
                    timestamp = timestamp + pad(Math.abs(this.getTimezoneOffset() / 60).toFixed(0));
                    timestamp = timestamp + ":" + pad(Math.abs(this.getTimezoneOffset() % 60).toFixed(0));
                }
                return timestamp;
            };

        }());
    }
    now = new Date();
    document.writeln("Create date object containing present time and print UTC version.");
    document.writeln(now.toUTCString());
    document.writeln("Show local time version");
    document.writeln(now.toLocalISOString());
</script>
</pre>
Gerard Ashton
  • 560
  • 4
  • 16