最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

datetime - Using javascript's date.toISOString and ignore timezone - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share edited May 29, 2015 at 14:39 chiapa asked May 29, 2015 at 11:30 chiapachiapa 4,41211 gold badges70 silver badges108 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 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.

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 puter 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/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>
发布评论

评论列表(0)

  1. 暂无评论