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
1 Answer
Reset to default 7Your 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
版权声明:本文标题:node.js - Javascript convert between timezones with momentjs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741707134a2393626.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论