admin管理员组

文章数量:1303054

in my app the user selects a date + time, and a timezone. This choice must be shown to other users IN THEIR OWN timezone, which means if user A from New York chooses August 8, 3:00 AM in EDT, user B from Beijing will see this datetime converted to China Standard Time (CST).

To do this I thought - ok I save user A's date and tell him to select out of 4 timezones. Server side I convert this from his EDT choice to UTC using moment timezone, and save it as UTC in the database, so that when I read it, I convert it back to other users according to their own timezones.

Is this a correct approach or am I overplicating things? THe purpose is that user A can choose her desired timezone, database saves as normalized UTC, target user sees in his own timezone.

(im using node.js for serverside btw) Thanks,

in my app the user selects a date + time, and a timezone. This choice must be shown to other users IN THEIR OWN timezone, which means if user A from New York chooses August 8, 3:00 AM in EDT, user B from Beijing will see this datetime converted to China Standard Time (CST).

To do this I thought - ok I save user A's date and tell him to select out of 4 timezones. Server side I convert this from his EDT choice to UTC using moment timezone, and save it as UTC in the database, so that when I read it, I convert it back to other users according to their own timezones.

Is this a correct approach or am I overplicating things? THe purpose is that user A can choose her desired timezone, database saves as normalized UTC, target user sees in his own timezone.

(im using node.js for serverside btw) Thanks,

Share Improve this question asked Aug 24, 2014 at 8:54 Amc_rttyAmc_rtty 3,81311 gold badges50 silver badges76 bronze badges 3
  • What's "normalized UTC"? You'll have to store the timezone, don't you? Like "CST" or -6. – Rudie Commented Aug 24, 2014 at 9:00
  • 1 Your approach is absolutely correct. – Scott Cramer Commented Aug 24, 2014 at 9:04
  • @Rudie - yes, correct; i figured i store the date as UTC, and a separate field iwth timezone "CST". – Amc_rtty Commented Aug 24, 2014 at 11:25
Add a ment  | 

1 Answer 1

Reset to default 7

Your approach is correct : save the time in UTC in the database and display it to the users according to their timezones.

momentjs can help you to do it but you need to give it the file of historical timezone changes.

check http://momentjs./timezone/ for further explanations.

once you have this, it is as easy as

var jun = moment("2014-06-01T12:00:00Z");
jun.tz('America/Los_Angeles').format('ha z');  // 5am PDT

also: don't store the user timezone as EDT, CST or -6. The hour shift can change with Summer times, Winter times, .. you need to store the Olson format, 'America/Los_Angeles'

本文标签: nodejsJavascript convert between timezones with momentjsStack Overflow