spencermountain / spacetime

A lightweight javascript timezone library
http://spacetime.how/
Other
3.99k stars 183 forks source link

spacetime().d not returning what I would expect #185

Closed jecraig closed 4 years ago

jecraig commented 4 years ago

new Date(spacetime(new Date()).hour(6).goto('America/Los_Angeles').hour(4).epoch) Thu Jan 23 2020 05:01:21 GMT-0700 (Mountain Standard Time)

spacetime(new Date()).hour(6).goto('America/Los_Angeles').hour(4).d Thu Jan 23 2020 04:01:30 GMT-0700 (Mountain Standard Time)

I thought the .d property would return the correct Date object based on the epoch in spacetime. It looks like it's returning the same time instead of the local time.

Using version 6.0.1

spencermountain commented 4 years ago

hey @jecraig yes, the Date object used internally is the local time, before the offset is applied. That's part of why I didn't include it in the api originally.

Maybe we should have a .toDate() function, and a .toLocalDate(), or something, to clear this up. Any ideas?

related to #180 - it seems to be a confusing aspect of the API

jecraig commented 4 years ago

That would be preferable. We can mark the .d as obsolete and users can continue to use it if they want.

jecraig commented 4 years ago

I've held off correcting the issue in our codebase pending a proper function.

As to which function, I'd vote for .toLocalDate().

spencermountain commented 4 years ago

cool. in the meantime you should be able to do:

let s = spacetime()
let d = new Date(s.iso())

(i think)

jecraig commented 4 years ago

new Date(s.epoch) works as well, but I don't want to patch that in and then repatch with the proper solution.