spencermountain / spacetime

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

How to get current time in current timezone? #219

Closed erjdriver closed 4 years ago

erjdriver commented 4 years ago

Apologies - I tried to look for a solution and couldn't find it.

How do I get the current time in the current timezone or how do I get the current timezone?

spacetime.now() gets the utc time -

or if I knew the current timezone, I could do

spacetime.now(timezone)

Thanks.

jecraig commented 4 years ago

I'm not sure what you're trying to do. The spacetime library will just default to whatever your browser time is to.

image

erjdriver commented 4 years ago

I'm trying to use get the current time for the current timezone.

I'm using in the context of an app - the app is open and user changes timezones (in the phone or flies there) and I'd like to get the new local time in the local timezone...iow...i'm using a npm to create an android app and using it.

e.g. someone flies from NY to Denver.

spacetime.now() gets utc

The user can be in any timezone - I'd like to know that - localtime in local timezone.

jecraig commented 4 years ago

Make sure your timezones have the correct tz names. To see the list that spacetime supports you can use spacetime.now().timezones

And here's an example in use image

erjdriver commented 4 years ago

When I do spacetime.now(), I get

{ JS: "epoch": 1597014104271, JS: "tz": "utc", JS: "silent": true, JS: "_weekStart": 1 JS: }

Note the tz is utc - not my timezone.

That's the problem I'm trying to solve.

I'm not using this lib in a browser. I'm using it to develop an Android app using Nativescript framework. I added this lib to the project using npm.

jecraig commented 4 years ago

Try doing spacetime.now('america/new_york') or spacetime.now('america/denver') and see if the time that comes back with different times.

erjdriver commented 4 years ago

That works.

But I'm trying to get the current device/phone timezone and time.

Looking at the source code

spacetime/src/timezone/guessTz.js

It seems if Intl object is not defined - then default of utc is used.

spencermountain commented 4 years ago

@erjdriver yeah, if the browser doesn't support Intl, you may need to fallback to a 3rd party geolocation service

some use maxmind, though i have no experience with it cheers