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.
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.
The date is parsed correctly, it's just toString that displays the timestamp in your local timezone:
let s = "2005-07-08T11:22:33+0000";
let d = new Date(Date.parse(s));
// this logs for me
// "Fri Jul 08 2005 13:22:33 GMT+0200 (Central European Summer Time)"
// and something else for you
console.log(d.toString())
// this logs
// Fri, 08 Jul 2005 11:22:33 GMT
// for everyone
console.log(d.toUTCString())
Javascript Date object are time values - they merely contain a number of milliseconds since the epoch. There is no timezone info in a Date object. Which calendar date (day, minutes, seconds) this timestamp represents is a matter of the interpretation (one of to...String methods).
The above example shows that the date is being parsed correctly for offset +0000 - that is, it actually contains an amount of milliseconds corresponding to "2005-07-08T11:22:33" in GMT.
What is the correct way to write a Date in JavaScript without ...
Date and time without timezone
How to parse a date string as a specific time zone
Question regarding dates and time zones
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)?
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:
Use the an alternate type like integer value, of milliseconds or days (since 1970-01-01), or string in
YYYYMMDDformat.This was combined with #1Use
Dateignoring time (as 00:00 local TZ). Convert from/to UTC when reading/writing to databaseUse
Datewith time as 00:00 UTC. Have to be careful not to mix withDatevalues in local TZ (e.g.now = new Date())Use
Datein local TZ, but convert to UTC when read/writing to database. This is a variant of #3.Create a
LocalDateclass that enforces midnight.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.