-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a plugin with the time zone support #323
Comments
Here's another slightly different use-case. I'm working on a project that needs to display a series of measurements (things like a lake's water level) recorded at different sites spread across a few different timezones. When creating a site, I make a note of which timezone it's located in. Then, when I'm showing a table of measurements to the user, I want to show the measurements' times of recording in the site's timezone, because then the user can think about them in relation to the local time of day. (i.e., does the lake level rise highest an noon, or in the evening?) It sounds like your proposal above should be able to handle it. It's basically the same as your business case 3, except that the timezone is not part of the user's settings. |
Yes, @agwells, if you do not store the dates in UTC, but in the original time zone together with the time zone name, you can print the date in the original zone later. For example: const measurements = [
{ value: 123.45, date: '2018-10-07 10:30', timeZone: 'Europe/Prague' },
{ value: 198.75, date: '2018-10-07 16:00', timeZone: 'Europe/London' }
]
// Print dates in the original time zone
measurements.forEach(({ value, date, timeZone }) => {
const localDate = dayjs(date, { timeZone })
const formattedDate = localDate.format('MM/DD/YYYY h:mm A [GMT]Z (z)', { timeZone })
console.log(`${value} at ${formattedDate}`)
})
// Print dates in the single chosen time zone
const displayTimeZone = 'Europe/Lisbon'
measurements.forEach(({ value, date, timeZone: originalTimeZone }) => {
const localDate = dayjs(date, { timeZone: originalTimeZone })
const formattedDate = localDate.format('MM/DD/YYYY h:mm A [GMT]Z (z)', { timeZone: displayTimeZone })
console.log(`${value} at ${formattedDate}`)
}) Do you store the time zone information as the canonical time zone name ("Europe/Berlin", for example), or as the UTC offset at the particular day ("GMT+02:00 CEST", for example)? |
We do this:
So it looks like that's actually a simpler use-case than in your example code. :) Something like this... const measurements = [
{ value: 123.45, date: '2018-10-07T08:30:00Z', timeZone: 'Europe/Prague' },
{ value: 198.75, date: '2018-10-07T16:00:00Z', timeZone: 'Europe/London' }
];
// Print dates in the original time zone
measurements.forEach(({ value, date, timeZone: originalTimeZone }) => {
const utcDate = dayjs(date, {timeZone: 'UTC'})
const formattedDate = utcDate.format('MM/DD/YYYY h:mm A [GMT]Z (z)', { timeZone: originalTimeZone })
console.log(`${value} at ${formattedDate}`)
}) ... and more or less the inverse operation, when parsing user input to convert it into UTC. |
I do this:
|
Yes @agwells, it will work fine. Just a little note - if you always include the "Z" in your stored date strings, you won't need to specify the time zone explicitly as "UTC", when parsing the date strings to const utcDate = dayjs(date) You will save a couple of characters and gain an immeasurable performance improvement :-) |
The code formatting a date an an arbitrary time zone is cunning, @xxyuk :-) Providing, that the Node.js or the browser supports time zones. dayjs(new Date().toLocaleString("en-US", {timeZone: "America/New_York"})).format('h:mA'); Just make sure, that you use the |
We were also using |
I've created a small ridiculous plugin to use locally in the project that I currently work, inspired on date-fns-tz/Intl API. If anyone wants to check it out and maybe give some feedback, it is really simple and could be a way to solve timezone problem with dayjs. |
Yikes this has put me off migrating dayjs from moment. I'm surprised there isn't an official plugin already. If there is no easy equivalent of this code, it's a problem:
|
This looks like a possible alternative for those that need a solution now! But note that this will not help to render the time-zone correctly via the I'd add that |
You sound about right! In my case, all I need to do are operations like adding/subtracting but considering the timezone. |
Any news on this ? |
The fact that this isn't closed almost 2 years later is a bummer... Was really considering migrating to dayjs from moment, but this is a must. |
Day.js Time Zone Plugin https://day.js.org/docs/en/timezone/timezone |
Is there a use example ? I tried:
It seems that from source code, tz() returns an offset, should this be used with a function like dayjs().add(offset) ? |
@Herz3h Please check here, https://day.js.org/docs/en/timezone/timezone Note you have to |
I did this:
and used code in previous post. I get a number as a return value instead of a |
Timezone plugin needs UTC plugin var dayjs = require("dayjs")
var utc = require("dayjs/plugin/utc")
var timezone = require("dayjs/plugin/timezone")
dayjs.extend(timezone)
dayjs.extend(utc)
dayjs().tz('Europe/Paris').format('DD/MM/YYYY') |
@Herz3h Check my comment here, please. #323 (comment) |
Is there any dayjs support in Google App Scripts library including timezone plugin? |
i have extended dayjs with timezone plugin like this
But for some reason format method is returning wrong answer From Expected But instead got or
Expected But instead got |
Update: Day.js Time Zone Plugin https://day.js.org/docs/en/timezone/timezone
Business Case
A typical application, which displayed dates and expects dates entered by the users handles the time zone:
Interface Synopsis
A minimal support could include the following two scenarios, typical for applications displaying and editing dates:
The API for these scenarios could use the
constructor
and theformat
method:Implementation Proposal
Day.js uses an embedded
Date
object. That is why this library is so lightweight, but it limits its functionality. TheDate
object supports only local time zone and UTC. It does not work in other time zones. It is possible to continue leveraging this principle. without making more complicated by replacing internalDate
object with something else. All other methods continue working as expected.dayjs(input: any, { timeZone: boolean }?)
Day.js already supports an optional
options
object in the constructor and theparse
method. The time zone parameter in the constructor is meant only for converting the parsed input string correctly to UTC. The embeddedDate
object will be initialised with UTC and offer the local time zone representation as usual. The original time zone offset will not be remembered. It is usually not important, because dates should be rendered consistently in user's time zone; not in various time zones, which their string sources referred to.format(format: string, { timeZone: boolean }?)
The
options
object is optionally recognised by the overriddenformat
method. The time zone parameter will extract the date parts (year, month, ...) from the embeddedthis.$d
object in UTC and convert them to the specified time zone, before producing the output string.This can be delivered as a new plugin. Not everyone need the full time zone handling and if the implementation includes the time zone data, it will not be small.
The text was updated successfully, but these errors were encountered: