简体   繁体   English

Moment.js:将UTC日期从数据库转换为本地时间

[英]Moment.js: Convert UTC date from database into local time

I'm attempting to use moment.js to convert my dates on the client side. 我正在尝试使用moment.js在客户端转换我的日期。 I'm going to give the exact specifications. 我打算给出确切的规格。

Dates are stored on the server using DateTime.UtcNow in C#. 使用C#中的DateTime.UtcNow将日期存储在服务器上。 They are saved to a database and look like this: 2014-06-02 21:37:40.087 , so approximately 9:37PM in UTC time. 它们保存到数据库中,如下所示: 2014-06-02 21:37:40.087 ,所以在UTC时间大约9:37 PM。 I'm in the West Coast of the US so that translates to 2:37PM PST. 我在美国西海岸,所以转换到太平洋标准时间下午2:37。

When this rows is read from the application layer and sent to the browser in JSON it resolves to this string "2014-06-03T04:37:40.087Z" . 当从应用程序层读取这些行并以JSON格式发送到浏览器时,它将解析为此字符串"2014-06-03T04:37:40.087Z"

What's with the discrepancy in how it's stored in the database vs. how it comes out in JSON text? 它存储在数据库中的方式与JSON文本中的方式有​​何不一致?

It would appear that even though you are storing UTC values, you aren't treating it as UTC when retrieving those values from the database. 即使您正在存储 UTC值,您也不会在从数据库中检索这些值时将其视为UTC。 Make sure your C# DateTime values have DateTimeKind.Utc after reading, using DateTime.SpecifiyKind . 确保你的C# DateTime值具有DateTimeKind.Utc阅读,使用后DateTime.SpecifiyKind

If you'd like to edit your question with some additional details, such as the specific code involved, then I may be able to provide a clearer answer. 如果您想用一些其他详细信息(例如所涉及的特定代码)编辑您的问题,那么我可以提供更清晰的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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