在Azure表存储中存储DateTime [英] Storing DateTime in azure table storage
问题描述
我正在使用默认示例将日期时间值存储在表存储中.其中一个字段的计算方式如下
I am using default example for storing a datetime value in table storage. One the field is calculated as follows
DateTime accMonth = new DateTime(DateTime.Now.Year,
DateTime.Now.Month, 1);
通常,上面的意思是日期为00:00的日期.
Usually above means a date with time being 00:00 .
但是,当我将其保存在表存储中时,这次我看到的是
However when I save this in table storage I see this time as
2018-04-01T18:30:00.000Z
对我来说这看起来很奇怪!有人知道为什么吗?
Which looks strange to me! anyone knows why?
推荐答案
您将获得不同的值,因为您正在创建具有本地时区的日期/时间(印度为GMT + 5:30).在Azure存储中,日期/时间值另存为UTC.
You're getting a different value because you're creating a date/time with local time zone (India is GMT+5:30). In Azure Storage, date/time values are saved as UTC.
SDK所做的就是将此日期转换为UTC,然后保存该日期.这就是为什么您在设置的日期/时间值之前5:30小时看到日期/时间值的原因.
What the SDK is doing is converting this date into UTC and then saving that date. That's why you're seeing a date/time value 5:30 hours before the date/time value you're setting.
要解决此问题,请将日期/时间类型指定为UTC.这样,SDK将不会进行任何转换.因此您的代码将是:
To solve this problem, specify the date/time kind as UTC. Then the SDK will not do any conversion. So your code would be:
var accMonth = new DateTime(DateTime.Now.Year,
DateTime.Now.Month, 1, 0, 0, 0, DateTimeKind.Utc);
这篇关于在Azure表存储中存储DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!