That approach is not that reliable, as MDN explains:ĭate instances refer to a specific point in time. Note that many other answers to this question attempt to obtain the same information by calling Date.toString(). In Colombia, you'd get Colombia Standard Time, vs. In California right now, toTimeString() returns Pacific Daylight Time while the Intl API returns America/Los_Angeles. If possible in your scenario, I would recommend utilizing a javascript library like date-fns, luxon or dayjs which provide timezone support.Ĭonsole.log(new Date().toTimeString().slice(9)) Ĭonsole.log(Intl.DateTimeFormat().resolvedOptions().timeZone) Ĭonsole.log(new Date().getTimezoneOffset() / -60) This will give you "GMT-0400 (EST)" for example, including the timezone minutes when applicable.Īlternatively, with regex you can extract any desired part:įor "GMT-0400 (EDT)" : new Date().toString().match(/(++.*)/)įor "GMT-0400" : new Date().toString().match(/(++)/)įor just "EDT" : new Date().toString().match(/\((.*)\)/)įor just "-0400": new Date().toString().match(/(+)\s/)ĮDIT - The above solution may not work in all browsers and locales. Var timeZoneFormatted = split + " " + split If you want the client timezone nicely formatted you can rely on the JavaScript Date.toString method and do: var split = new Date().toString().split(" ") I realize this answer is a bit off topic but I imagine many of us looking for an answer also wanted to format the time zone for display and perhaps get the zone abbreviation too. TimeZone property undefined in this case.Įdit 3-19-2022 - WARNING: I no longer recommend this approach as it has issues with multiple browsers and locales. TimeZone property was provided in the options object provided to the TimeZone property will be the name of the default time zone if no In this version of the ECMAScript 2015 Internationalization API, the In ecma-402/3.0 which is still in a draft it changed to Versions may return a String value identifying the host environment’s However, applications should not rely on this, as future Provided in the options object provided to the Intl.DateTimeFormatĬonstructor. TimeZone property will remain undefined if no timeZone property was In this version of the ECMAScript Internationalization API, the However, future draft (3.0) fixed that issue by changing to system default timezone. Old compatibility informationĮcma-402/1.0 says that timeZone may be undefined if not provided to constructor. NET has the equivalent DateTimeOffset type so you wouldn't need to make any conversion in client (Intl.DateTimeFormat().resolvedOptions().timeZone) As of April 2023, this works in 95.42% of the browsers used globally. SQL Server allows comparisons, filtering, calculations etc on values of different offsets, so you wouldn't need to make any conversions for querying. You could probably avoid all conversions if you used the datetimeoffset type instead of datetime. The first AT TIMEZONE returns a datetimeoffset with a +2:00 offset and the second converts it to UTC. ![]() You still need a double conversion though, first to the local timezone then to UTC: SELECT (getdate() at time zone 'Central Europe Standard Time') AT TIME ZONE 'UTC' Assuming your field is called SomeTime, you could write select SQL Server 2016 you can use the timezone names. Switch to another offset (eg UTC) is performed by the SWITCHOFFSET function select can combine both with select SWITCHOFFSET(TODATETIMEOFFSET(GETDATE(),120),0) This will return a datetimeoffset value with the original time and the specified offset. Or select TODATETIMEOFFSET(GETDATE(),120) Once you have it you can convert a datetime to a `datetimeoffset with a specific offset with TODATETIMEOFFSET: select TODATETIMEOFFSET(GETDATE(),'02:00') In versions up to SQL Server 2014, you have to determine the correct offset for your local timezone in advance, eg using C# code. Your question has two parts - how to convert a datetime value to a value with offset/timezone and then how to convert that value to a UTC. Timezone support was added to SQL Server in the latest version, 2016. A timezone can have different offsets if daylight savings time is used. Timezone and timezone offset are two different things.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |