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
🌐
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 ·
Discussions

Why .toISOString() gives different time?
It's because the output is Greenwich Time, and you are in another timezone. ... You're getting a date in the UTC time zone (that's what "Z" means at the end of the time string). ... One choice would be as the epoch. ... The toISOString() method always outputs the time in UTC. More on stackoverflow.com
🌐 stackoverflow.com
Date object and timezones stuff
You are correct that JavaScript looks at new Date(‘2024-02-06’) and coverts it based on the time as 00:00:00 GMT, so it is the same as new Date(‘2024-02-06T00:00:00Z’) If you add “00:00:00” to your date string it should work. Example “new Date(‘2024-02-06 00:00:00’)” will be February 6, 2024 at 12:00am for all users regardless of their time zone. If I recall correctly you can also change your date string format to mm/dd/yyyy and it will covert as you expect with new Date. So new Date(‘02/06/2024’) would also be February 6, 2024 for all users regardless of time zone. More on reddit.com
🌐 r/learnjavascript
10
2
February 6, 2024
formatISO doesn't use UTC (Z) timezone
When making a sanity check of now.toISOString() === formatISO(now) (with const now = new Date()), it fails due to a timezone difference, as the formatISO doesn't match the browser's toISOString, which explicitly claims that The timezone is always zero UTC offset, as denoted by the suffix "Z".. More on github.com
🌐 github.com
23
January 12, 2021
I do I get Local time using Date().toISOstring() - Programming & Development - Spiceworks Community
I want to extract current localtime. by using the below code, I get incorrect time var t=new Date(); console.log(t.toISOString()); More on community.spiceworks.com
🌐 community.spiceworks.com
3
February 1, 2017
🌐
GitHub
gist.github.com › lvl99 › af20ee34f0c6e3984b29e8f0f794a319
Date ISO string without timezone information · GitHub
Date ISO string without timezone information · Raw · date-iso-string-without-timezone.js · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below.
🌐
DEV Community
dev.to › shubhampatilsd › removing-timezones-from-dates-in-javascript-46ah
Removing Timezones from Dates in Javascript - DEV Community
April 23, 2023 - Now, despite the timezone information still being there in the console.log, if we run the same code in Honolulu, we'll get · Wed Feb 22 2023 16:30:00 GMT-1000 (Hawaiian Aleutian Time) The two dates line up! Now here is the actual implementation for the pseudocode: export const dateWithoutTimezone = (date: Date) => { const tzoffset = date.getTimezoneOffset() * 60000; //offset in milliseconds const withoutTimezone = new Date(date.valueOf() - tzoffset) .toISOString() .slice(0, -1); return withoutTimezone; }; First tzoffset initializes a new Date and get the timezone offset in milliseconds (in my case, it would be 28800000 milliseconds).
🌐
W3Schools
w3schools.com › jsref › jsref_toisostring.asp
W3Schools.com
The toISOString() method returns a date object as a string, using the ISO standard.
🌐
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 - The code snippet creates a Date object representing the current date and time and then converts it to an ISO string using the toISOString() method. The resulting ISO string will include both date and time components.
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-create-date-without-timezone
How to Create a Date without Timezone in JavaScript | bobbyhadz
March 6, 2024 - 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.
🌐
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.
Find elsewhere
🌐
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'; }; })(); }
🌐
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.
🌐
Reddit
reddit.com › r/learnjavascript › date object and timezones stuff
r/learnjavascript on Reddit: Date object and timezones stuff
February 6, 2024 -

when I set a date using a calander libary and save it in my localstorage as date.toISOString().split("T")[0] and after reloading the page, I retrieve from the localstorage and set in my local variable with new Date(date), it sets the time 1 day backward. I'm guessing its because the timezone i'm using is US and since US is 7 hours behind UTC, so new Date() treats the argument as a UTC time and converts it into a local time?, how do I configure it to give me the exact date I stored in my localstorage?

🌐
Full Stack Foundations
fullstackfoundations.com › blog › javascript date tutorial: get the timezone right!
JavaScript Date Tutorial: Get the Timezone Right!
March 29, 2024 - A JavaScript date is the number of milliseconds that have passed since midnight on January 1st, 1970 (UTC timezone). ... When working with JavaScript dates, you must ALWAYS remember this definition. There are plenty of prototype functions on the Date object such as getDate(), getUTCDate(), and toISOString, but there is only 1 method that gives you the true representation of a JavaScript Date:
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Date › getTimezoneOffset
Date.prototype.getTimezoneOffset() - JavaScript | MDN
In regions that use DST, the return value may change based on the time of the year date is in. Below is the output in a runtime in New York, where the timezone is UTC-05:00.
🌐
GitHub
github.com › date-fns › date-fns › issues › 2151
formatISO doesn't use UTC (Z) timezone · Issue #2151 · date-fns/date-fns
January 12, 2021 - When making a sanity check of now.toISOString() === formatISO(now) (with const now = new Date()), it fails due to a timezone difference, as the formatISO doesn't match the browser's toISOString, which explicitly claims that The timezone is always ...
Author   scscgit
🌐
Medium
mayowaobisesan.medium.com › difference-between-toisostring-and-toutcstring-in-javascript-date-class-3188f353dbf9
Difference between toISOString and toUTCstring in Javascript Date Class. | by Mayowa Obisesan | Medium
May 18, 2024 - To put simply toUTCString() is a JavaScript Date method that returns date using the Universal TimeZone. toUTCString() is similar to toISOString() in the sense that both methods return date and time, but in different formats.
🌐
Hinode
flypenguin.de › home › 2021st › 8th › 18th › javascript iso date with local timezone
- JavaScript ISO date with local timezone
July 11, 2024 - function getISOLocalString() { let date = new Date(); let tzo = -date.getTimezoneOffset(); if (tzo === 0) { return date.toISOString(); } else { let dif = tzo >= 0 ?
🌐
Day.js
day.js.org › docs › en › plugin › timezone
Timezone · Day.js
const d2 = dayjs.utc("2013-11-18 11:55").tz("Asia/Taipei"); d2.format(); // => 2013-11-18T19:55:00+08:00 d2.toISOString(); // => 2013-11-18T11:55:00.000Z ... // Setting the default timezone dayjs.tz.setDefault("America/New_York"); // Resetting the default timezone to the system timezone dayjs.tz.setDefault();
🌐
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());
🌐
CoreUI
coreui.io › blog › how-to-manage-date-and-time-in-specific-timezones-using-javascript
How to Manage Date and Time in Specific Timezones Using JavaScript · CoreUI
January 22, 2025 - JavaScript’s Date object provides methods like getTimezoneOffset to calculate timezone differences. const utcDate = new Date("2025-01-22T17:00:00Z") // UTC time const offset = utcDate.getTimezoneOffset() * 60000 // Offset in milliseconds const localDate = new Date(utcDate.getTime() - offset) console.log(localDate.toISOString()) // ISO string in local timezone