简体   繁体   中英

convert date based on timezone user is in

I have a date I want to convert based on their timezone.

For example, I want to set it in EST time (America/New_York)

2019-04-24 12:00:00

and if the user comes across the site from America/Los_Angeles, it will appear:

2019-04-24 09:00:00

I need to be able to return the hour, so in that example: 9.

I tried using https://github.com/iansinnott/jstz to determine their timezone and https://moment.github.io/luxon in hopes of handling the conversion w/o any luck.

I was testing by changing the timezone on my computer w/o any luck.

You could keep a list of timzeone identifiers and a list of their corresponding +/- number of hours with respect to your local time (which is returned by your time function).

Once you have a user's time zone, and you have extracted the current hour from the local timestamp simply look up the timezone in your list and use it's index to access the second list to find how many hours to add or subtract from the users time.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString

var date = new Date(Date.UTC(2012, 11, 12, 3, 0, 0));

// toLocaleString() without arguments depends on the implementation,
// the default locale, and the default time zone
console.log(date.toLocaleString());
// → "12/11/2012, 7:00:00 PM" if run in en-US locale with time zone America/Los_Angeles

Or you can use getYear , getMonth , getDate , getHours , getMinutes , getSeconds to format your own representation of the date. These methods all return values according to the user's local timezone.

Converting date based on the time can be done like this. reference convert date to another time zone example snippet is under.

 var usaTime = new Date().toLocaleString("en-US", {timeZone: "America/New_York"}); usaTime = new Date(usaTime); console.log('USA time: '+usaTime.toLocaleString()) var usaTime = new Date().toLocaleString("en-US", {timeZone: "America/Los_Angeles"}); usaTime = new Date(usaTime); console.log('USA time: '+usaTime.toLocaleString()) 

It sounds like you're asking to convert from a specific time zone to the user's local time zone (whatever it may be). You do not need time zone detection for that, but at present you do need a library. (Answers that suggest using toLocaleString with a time zone parameter are incorrect, as that function converts to a specific time zone, but cannot go the other direction.)

Since you mentioned Luxon , I'll provide a Luxon specific answer:

luxon.DateTime.fromFormat('2019-04-24 12:00:00',       // the input string
                          'yyyy-MM-dd HH:mm:ss',       // the format of the input string
                          { zone: 'America/New_York'}) // the time zone of the input
              .toLocal()                               // convert to the user's local time
              .toFormat('yyyy-MM-dd HH:mm:ss')         // return a string in the same format

//=>  "2019-04-24 09:00:00"

This capability is also provided by other libraries, such as date-fns-timezone , js-Joda , or Moment-Timezone , but it is not yet something built in to JavaScript.

I think the question may need more clarification - my first impression was you refer to a date-time that you already have and serve from the server. Doesn't this problem boil down to the Date object being "user-timezone-aware"? or not? But it is (some methods are, to be exact)

Your date/time is 2019-04-24 12:00:00 EDT (i assume PM) This means the Unix timestamp of this in milliseconds is 1556121600000 (i assume daylight is on for April so not pure EST but EDT and an offset of UTC-4:00)

When you call

 console.log(new Date(1556121600000).getHours()) 

doesn't this return 9 as you suggest, for Javascript executed on a browser from America/Los_Angeles with PDT timezone?

As suggested at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getHours :

The getHours() method returns the hour for the specified date, according to local time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM