Reputation: 29
How can I calculate time based on a different location's time in my react native app? For example, user is in New York and I want to the app to convert the time user inserts and convert it based on another location's time (which is given in lat/long) and then send it to server to store in DB. Thanks for your time in advance.
Upvotes: 1
Views: 1449
Reputation: 29
What I needed to do was to be able to change the date/time based on a location entered by user. I initially thought of using lat/long, but then I realized Google Places returns offsetFromUTC and I used date.getTimezoneOffset() to calculate the offset of user's device, and then calculated the time that should actually go into DB.
Upvotes: 0
Reputation: 3690
Since by default Date.now()
will give you the time based on the phone's timezone, I would suggest using momentjs which has a feature to get the UTC time (more on the docs).
The default use would be:
const now = moment();
And in order to avoid timezones and get the UTC time:
const now = moment.utc();
Upvotes: 1