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
question - keeping calendar times from millisecond timestamps #59
Comments
You have three options: Option 1: Do something like you're suggesting above. The trick you're missing is that you need to store the zone from the clock in, not the zone from where you're displaying it. // let's say the clock in happens at 7am US Pacific Time
dt = DateTime.local()
dt.toUTC().toISO() //=> '2017-11-08T15:00:00.000Z' (can store however you want)
dt.zoneName //=> 'America/Los_Angeles' (also store this)
// then another client in, say, US Eastern time handles that time:
DateTime.fromISO( '2017-11-08T15:00:00.000Z').setZone('America/Los_Angeles').toLocaleString(DateTime.TIME_SIMPLE) //=>
'7:00 AM' In other words, if you want to display the times in a different local time, you need to know that other local time's zone. Depending on what you're storing the datetime in, you may or may not need to stash that time zone separately. Option 1 is the most thorough and flexible approach. Option 2: forget zones and just use offsets. As long as you don't need to do math on the times (e.g. adding or subtracting hours to and from it), this suffices: // in LA:
dt = DateTime.local()
dt.toISO() //=> '2017-11-08T07:00:00.000-08:00'
// in NY:
dt = DateTime.fromISO('2017-11-08T07:00:00.000-08:00', { setZone: true })
dt.toFormat('hh:mm') // 7:00
// although this is a bug!!
dt.toLocaleString(DateTime.TIME_SIMPLE) //=> '3:00 PM' (??) Not sure what's up with that last part. I'll have a look. Option 3: do the exact opposite of my recommendation re: UTC times and instead only store local times. If you're using something like Postgres, that's // in NY:
dt = DateTime.fromISO('2017-11-08T07:00:00.000') // notice no offset
dt.toLocaleString(DateTime.DATETIME_SHORT) //=> '11/8/2017, 7:00 AM' I don't really recommend this because if you need to do anything other than display it, you'll find yourself working with the wrong time. |
Thanks a lot for the detailed response. I think I was essentially implementing the equivalent of option two at first and not seeing any change, which may have been due to that bug you mentioned? |
Pulling this over from the other ticket:
No, you really shouldn't need to. If you've provided a specific epoch millisecond (or an ISO string with an offset or Z), that identifies the right global, universal time exactly, regardless of zone. The only thing you need to do is to tell that date time to render in the zone where that time is called 7am instead of whatever the browser's time zone tells you to call that time. So if you You'll need to post some code. |
OK thanks for clarifying that makes sense. So is it safe to say the toLocaleString method works like so:
I wasn't seeing the time being modified consitsently across timezones (it was jumping to an adjustment to the local time even after setting the zone) but I'll update to new version and see with my colleague overseas if he is seeing a change with the updated version and
thanks! |
That code should work, assuming that
Mostly. It is true that Also of use when thinking about that is this little snippet from the docs:
|
OK thanks. We're going to dry an do some debugging remotely with a colleague in another timezone, and have a deep look at what we're getting as a result at each step. Will get back with results, thanks! |
@SamMatthewsIsACommonName going to close this out. But LMK if you run into more trouble and we can reopen and continue. |
Hi,
I'm a bit confused by this but I'd be happy to make some documentation / examples once I understand it a bit more?
basically we store shift clock ins and clock outs as numerical Date.now() values. This enabled us to diff them very easily for lengths of shifts etc.
However as you can imagine problems arise when we try and display those times across different time zones. We want to keep the calendar time (e.g if they clocked in at 7am, we want it to show 7am everywhere).
I see this example:
Although I'm not 100% sure at what stage I'd set the zone in our case. My attempts to set it from the utc time or the millisecond time and then format them using toFormat() seem to just result in them being adjusted for the device's location (i.e the calendar times change)
I've read the docs quite a bit and I still can't figure out how this is possible (except that you recommend storing utc strings, so we will start storing these as well).
We also have access to the timezone of the user's device at each end, although I'm still a bit confused as to where and when we need that.
As I said, an example of how to do this would be amazing and then I could make a pull request showing it in a bit more detail if you'd like?
The text was updated successfully, but these errors were encountered: