I had the same issue. I get a date as a String, for example: '2016-08-25T00:00:00', but I need to have the Date object with correct time. To convert String into object, I use getTimezoneOffset:

var date = new Date('2016-08-25T00:00:00')
var userTimezoneOffset = date.getTimezoneOffset() * 60000;
new Date(date.getTime() + userTimezoneOffset);

getTimezoneOffset() will return ether negative or positive value. This must be subtracted to work in every location in world.

Answer from wawka on Stack Overflow
🌐
GitHub
gist.github.com › lvl99 › af20ee34f0c6e3984b29e8f0f794a319
Date ISO string without timezone information · GitHub
Save lvl99/af20ee34f0c6e3984b29e8f0f794a319 to your computer and use it in GitHub Desktop. Download ZIP · 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.
Discussions

What is the correct way to write a Date in JavaScript without ...
I'm not in the GMT timezone and new Date('1995-02-22') gives me new Date('1995-02-22') More on stackoverflow.com
🌐 stackoverflow.com
Date and time without timezone
Hi, I have a date field in Form. The selected date i need to pass it to integration service which is a datetime field. For my inputted date to pass i need to More on community.appian.com
🌐 community.appian.com
0
February 11, 2023
How to parse a date string as a specific time zone
I cannot quite find the right function to build a UTC date object from a date string, the format of the date string and the TZ Database name that the date in the date string is in. More on github.com
🌐 github.com
6
September 22, 2021
Question regarding dates and time zones
Had the same issue, our DB and servers are set to UTC but in browser they're set to user timezone, which JS always assumes. To ensure JS knows what timezone the time you're providing is in, append it to the end of your timestamp. For example: new Date(myTimestamp + " UTC"). Yep, looks horrific but is a valid and recommended solution apparently. Welcome to JavaScript! Also be sure to check Safari support as the wah Safari handles dates is very messed up. More on reddit.com
🌐 r/learnjavascript
17
2
August 10, 2023
🌐
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. /** * Formats the provided date in _ISO-8601 format_ without using * `toISOString()` to avoid timezone issues * * @param date The Date object * @returns {string} */ function formatDate(date) { return [ `${date.getFullYear()}`, `${date.getMonth() + 1}`.padStart(2, '0'), `${date.getDate()}`.padStart(2, '0') ].join('-') } Sign up for free to join this conversation on GitHub.
🌐
Stack Overflow
stackoverflow.com › questions › 73229951
What is the correct way to write a Date in JavaScript without ...
Dates in the format YYYY-MM-DD are parsed as UTC, so they do not have a timezone. Your issue is that Date.prototype.toString uses host system settings to produce a timestamp in the "local" timezone.
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-create-a-date-with-a-set-timezone-without-using-a-string-representation
How to create a date with a set timezone without using a string representation? | GeeksforGeeks
January 24, 2023 - The datetimezone object is sent as a parameter to the timezone_identifiers_list() function and it returns an in ... Converting a `DateTime` to a string in PHP involves formatting the `DateTime` object into a human-readable format using the `format()` method. This process allows you to represent dates and times as strings in various formats, such as Y-m-d H:i:s. There are some following approachesTable of ContentB ... Moment.js is the date library in JavaScript that provides parsing, validation, manipulation, and formatting of dates and times.
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-create-date-without-timezone
How to Create a Date without Timezone in JavaScript | bobbyhadz
March 6, 2024 - This is because my time zone is 3 hours ahead of Coordinated Universal time (UTC). 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.
🌐
Appian Community
community.appian.com › discussions › f › user-interface › 29410 › date-and-time-without-timezone
Date and time without timezone
February 11, 2023 - Hi, I have a date field in Form. The selected date i need to pass it to integration service which is a datetime field. For my inputted date to pass i need to
Find elsewhere
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Date › parse
Date.parse() - JavaScript | MDN
The following call, which does not specify a time zone will be set to 2019-01-01 at 00:00:00 in the local timezone of the system, because it has both date and time. ... Apart from the standard date time string format, the toString() and toUTCString() formats are supported:
🌐
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 · toISOString() None.
🌐
GitHub
github.com › date-fns › date-fns › issues › 2642
How to parse a date string as a specific time zone · Issue #2642 · date-fns/date-fns
September 22, 2021 - I cannot quite find the right function to build a UTC date object from a date string, the format of the date string and the TZ Database name that the date in the date string is in.
Published   Sep 22, 2021
🌐
Twilio
twilio.com › en-us › blog › converting-formatting-dates-timezones-javascript
Converting and Formatting Dates and Time Zones with JavaScript | Twilio
January 26, 2024 - This command creates a JavaScript file in your local file system named dates.js. You will use this file and Node.js to edit and run the code samples as you go. Next, open the dates.js file in your editor, paste in the following code, and be sure to save the modified file: ... In the code above, you’re creating a new Date object, converting it to a readable string, and printing it to the console.
🌐
Full Stack Foundations
fullstackfoundations.com › blog › javascript date tutorial: get the timezone right!
JavaScript Date Tutorial: Get the Timezone Right!
March 29, 2024 - And if I had to guess, a few of you reading this are in a similar spot, so cheers to that! 🍻 · JavaScript dates aren’t actually that difficult. As you read this post, you’ll realize that the difficult part of JavaScript dates is the non-JavaScript concepts like timezones and formatting standards.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Date › toLocaleTimeString
Date.prototype.toLocaleTimeString() - JavaScript | MDN
In implementations without Intl.DateTimeFormat support, this parameter is ignored. See the Intl.DateTimeFormat() constructor for details on these parameters and how to use them. A string representing the time portion of the given date according to language-specific conventions.
🌐
Reddit
reddit.com › r/learnjavascript › question regarding dates and time zones
r/learnjavascript on Reddit: Question regarding dates and time zones
August 10, 2023 -

We are trying to add times to our dates in an older app and the dates were almost always stored as partially formed ISO strings. Not a hard rule since times were never used before.

Now I need to start displaying the times with the dates and allowing the user to alter the times on the front end.

Example: we get a date ISO string from the backend as 2008-08-15T00:00:00.

When I create a Date object from it, I get the date in my local time zone (GMT-0600). In this example, Fri, August 15, 2008 00:00:00 (GMT-0600).

Then later when the edit form is submitted with no change to the day or time, I convert the Date object to an ISO string and strip the milliseconds and time zone code to keep it consistent with the current format in the database. In this example it returns 2008-08-15T06:00:00 to the backend.

Notice the time was provided as 00:00:00 but after parsing it and then converting to an ISO string, I've now added 6 hours to the time and am returning 06:00:00.

I am thinking I could convert it to GMT-0000 before converting to an ISO string but I'm not sure if that is the cleanest solution. Has anyone else had a similar scenario and what would you suggest to do to make this work (that doesn't include altering all the dates in the DB, we're planning for that down the road)?

Top answer
1 of 5
2
Had the same issue, our DB and servers are set to UTC but in browser they're set to user timezone, which JS always assumes. To ensure JS knows what timezone the time you're providing is in, append it to the end of your timestamp. For example: new Date(myTimestamp + " UTC"). Yep, looks horrific but is a valid and recommended solution apparently. Welcome to JavaScript! Also be sure to check Safari support as the wah Safari handles dates is very messed up.
2 of 5
2
new Date() defaults to using the local timezone pulled from the browser which is pulled from the OS. When dealing with times the best way to handle is to store dates in ISO or UTC, or if you want to store local times, store the IANA timezone string. ('America/Chicago' or 'America/Sao_Paulo') and always track the users locale string ('en-US', 'pt-BR) Then explicitly use both locale and IANA. So when you display on the frontend you can use something like this: new Date().toLocaleString( 'en-US', { dateStyle: 'medium', timeStyle: 'medium', timeZone: 'America/Chicago' } ) new Date() in the browser assumes UTC but toString converts to local. Give it a date without time new Date('2022-02-22') and it will convert FROM UTC TO your local standard time. The ASSUMPTION is this string comes from standard server time. date-fns and Luxon do the opposite. They assume '2022-02-22' is local time. parseISO('2022-02-22') without specifying timezone converts FROM local TO local. That's why they feel more intuitive if you're not paying attention. I HIGHLY encourage always using toLocaleString for formatting, even if you're using Luxon or date-fns. Don't give your users these manually formatted "MM/yyyy" things. Locale string formats are locale aware (sometimes dates go before moths), lowercase months when they're supposed to be lowercase, and handle translations. (Yeah! Translations!) So months days, weekdays, etc. will be translated. And for Spanish speaking countries they will use more standard things like 'de' which translates to 'of'. Aug 11, 2023, 2:32:00 PM 11 de ago. de 2023, 14:32:00
🌐
DEV Community
dev.to › shubhampatilsd › removing-timezones-from-dates-in-javascript-46ah
Removing Timezones from Dates in Javascript - DEV Community
April 23, 2023 - I decided that I would put this string (2/23/2023, 1:48:30 PM) in my database. I thought that I could then input this as a parameter to the Date constructor on the client, like so: ... I looked at both results and they were the same! I finally thought that this would be a great solution: store the output of .toLocaleString() and convert it to a Date again on the client, all without the timezone...
🌐
Reddit
reddit.com › r/askprogramming › what's are best practices when dealing with dates, without time-of-day?
r/AskProgramming on Reddit: What's are best practices when dealing with dates, without time-of-day?
May 19, 2022 -

What is best practice when dealing with date values without time, in JavaScript? My concern is subtle off-by-one bugs caused by local Time Zone (TZ) offset (e.g. +5 hours), when doing date math.

JavaScript's built-in Date type represents a date and time not just the date. Internal representation is an integer of microseconds since 1970-01-01 00:00:00 UTC. Other languages, like Python, have separate Date and DateTime types. Java 8 introduced LocalDate.

You also have things like: new Date('5/18/2020') is local TZ (in US), but new Date('2022-05-18') is UTC. Same with Date.parse(string). And the time zone on most servers in UTC, whereas on the browser side the time zone will vary.

The date values will be used for simple date math in code and will be stored in a SQL database.

Possibilities:

  1. Use the an alternate type like integer value, of milliseconds or days (since 1970-01-01), or string in YYYYMMDD format.

  2. This was combined with #1

  3. Use Date ignoring time (as 00:00 local TZ). Convert from/to UTC when reading/writing to database

  4. Use Date with time as 00:00 UTC. Have to be careful not to mix with Date values in local TZ (e.g. now = new Date())

  5. Use Date in local TZ, but convert to UTC when read/writing to database. This is a variant of #3.

  6. Create a LocalDate class that enforces midnight.

  7. Use a library. js-joda has LocalDate.

I am leaning towards #3 and #6. Some code I am writing:

class LocalDate extends Date {
  // Error if time isn't midnight local TZ
  // Do not accept string in ISO format
  constructor(date?: Date|number|string)

  // Convert to 00:00 UTC
  // To be used before write to database
  toUtc(): Date

  // Only return date.  Also affects toJSON()
  toISOString(): string

  // Returns today's date at 00:00 Local TZ
  static today(): LocalDate
  // Set time to midnight local TZ, without error check.
  static fromDateTime(date: Date|number): LocalDate
  // Convert to local TZ.  Error if not 00:00 UTC.
  static fromUtc(date: Date|number): LocalDate
}

UPDATE:

Various edits.

🌐
YouTube
youtube.com › hey delphi
JavaScript : Parse date without timezone javascript - YouTube
JavaScript : Parse date without timezone javascriptTo Access My Live Chat Page, On Google, Search for "hows tech developer connect"As I promised, I have a se...
Published   May 8, 2023
Views   35
🌐
Prismic
community.prismic.io › developing with prismic
Timestamp without timezone - Developing with Prismic - Prismic People
March 19, 2021 - When using the field timestamp I see that my timezone is displayed and affects the time that I am setting up. We would like to use timestamp without timezone because we want to display all our events in a fixed timezone …
🌐
Ursahealth
ursahealth.com › new-insights › dates-and-timezones-in-javascript
Working with dates and timezones in JavaScript: a survival guide
May 4, 2021 - Not that strings are a perfect choice for representing “wall time”, especially if we have to run some date calculations down the road, such as determining the earliest too-early time among a group of people. Now that we’ve established that there are some circumstances in which we do not want to automatically localize, let’s see how sensibly the JavaScript language approaches this problem.