moment.js is great but sometimes you don't want to pull a large number of dependencies for simple things.

The following works as well:

    var tzoffset = (new Date()).getTimezoneOffset() * 60000; //offset in milliseconds
    var localISOTime = (new Date(Date.now() - tzoffset)).toISOString().slice(0, -1);
    
    console.log(localISOTime)  // => '2015-01-26T06:40:36.181'

The slice(0, -1) gets rid of the trailing Z which represents Zulu timezone and can be replaced by your own.

Answer from yegodz on Stack Overflow
🌐
DEV Community
dev.to › shubhampatilsd › removing-timezones-from-dates-in-javascript-46ah
Removing Timezones from Dates in Javascript - DEV Community
April 23, 2023 - Now here is the actual implementation ... * 60000; //offset in milliseconds const withoutTimezone = new Date(date.valueOf() - tzoffset) .toISOString() .slice(0, -1); return withoutTimezone; };...
🌐
GitHub
gist.github.com › lvl99 › af20ee34f0c6e3984b29e8f0f794a319
Date ISO string without timezone information · GitHub
Date ISO string without timezone information. GitHub Gist: instantly share code, notes, and snippets.
🌐
GitHub
gist.github.com › barbietunnie › 67d10b59f7716e3ff5892d70dd9a3cfa
Format JS Date in ISO-8601 without timezone issues · GitHub
The following function provides an easy way to format Javascript dates in ISO-8601 format without using date.toISOString(), which could lead to timezone issues as the date is first converted to UTC which could lead to discrepancies in the result in certain timezones.
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-create-date-without-timezone
How to Create a Date without Timezone in JavaScript | bobbyhadz
To create a Date without the timezone, we called the toISOString() method on the Date object and removed the character Z from the ISO string. The Date object shows the exact same time as the one stored in the dateStr variable - 09:35:31.
🌐
W3Schools
w3schools.com › jsref › jsref_toisostring.asp
JavaScript Date toISOString() Method
The toISOString() method returns a date object as a string, using the ISO standard.
🌐
UsefulAngle
usefulangle.com › post › 30 › javascript-get-date-time-with-offset-hours-minutes
How to get DateTime with Timezone Offset (8601 format) in Javascript
Javascript has a toISOString method that gives a datetime in ISO-8601 format. But it gives datetime strings only in UTC format, YYYY-MM-DDTHH:mm:ss.sssZ. It does not give a timezone offset in hours and minutes.
Top answer
1 of 2
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.

2 of 2
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>
Find elsewhere
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-get-iso-date-without-time
Get ISO Date without Milliseconds or Time in JavaScript | bobbyhadz
Copied!// ✅ If you have a Date object const date = new Date(); const withoutMs = date.toISOString().split('.')[0] + 'Z'; console.log(withoutMs); // 👉️ "2023-07-27T05:08:50Z" // ✅ If you have a plain ISO string const isoStr = '2022-07-21T09:35:31.820Z'; const withoutMilliseconds = isoStr.split('.')[0] + 'Z'; console.log(withoutMilliseconds); // 👉️ "2022-07-21T09:35:31Z"
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Date › toISOString
Date.prototype.toISOString() - JavaScript | MDN
const event = new Date("05 October 2011 14:48 UTC"); console.log(event.toString()); // Expected output: "Wed Oct 05 2011 16:48:00 GMT+0200 (CEST)" // Note: your timezone may vary console.log(event.toISOString()); // Expected output: "2011-10-05T14:48:00.000Z" js ·
🌐
MSR
rajamsr.com › home › the best way to convert javascript date to iso string
The Best Way to Convert JavaScript Date to ISO String | MSR - Web Dev Simplified
February 24, 2024 - But dates and time zones can be hard to handle, especially if you want to use a standard format like ISO 8601. How can you turn a JavaScript date into an ISO string? You can use the toISOString() method, which is part of the Date object in ...
🌐
GitHub
gist.github.com › loilo › 736d5beaef4a96d652f585b1b678a12c
ISO 8601 date string – like Date.prototype.toISOString(), but with local timezone offset
ISO 8601 date string – like Date.prototype.toISOString(), but with local timezone offset · Raw · get-local-iso-string.js · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
Medium
tulusibrahim.medium.com › toisostring-return-utc-time-b2f473ceca2a
.toISOString() return UTC Time. So, I just encounter a weird bug in my… | by Tulusibrahim | Medium
October 27, 2022 - JavaScript — changing hours, when converting in ISO format · The answer says that the .toISOString() method will convert the time to UTC.
🌐
Reality Ripple
udn.realityripple.com › docs › Web › JavaScript › Reference › Global_Objects › Date › toISOString
Date.prototype.toISOString() - JavaScript
if (!Date.prototype.toISOString) { (function() { function pad(number) { if (number < 10) { return '0' + number; } return number; } Date.prototype.toISOString = function() { return this.getUTCFullYear() + '-' + pad(this.getUTCMonth() + 1) + '-' + pad(this.getUTCDate()) + 'T' + pad(this.getUTCHours()) + ':' + pad(this.getUTCMinutes()) + ':' + pad(this.getUTCSeconds()) + '.' + (this.getUTCMilliseconds() / 1000).toFixed(3).slice(2, 5) + 'Z'; }; })(); }
🌐
W3cubDocs
docs.w3cub.com › javascript › global_objects › date › toisostring
Date.toISOString - JavaScript - W3cubDocs
The toISOString() method of Date instances returns a string representing this date in the date time string format, a simplified format based on ISO …
🌐
Spiceworks
community.spiceworks.com › programming & development
I do I get Local time using Date().toISOstring() - Programming & Development - Spiceworks Community
February 1, 2017 - I want to extract current localtime. by using the below code, I get incorrect time var t=new Date(); console.log(t.toISOString());