简体   繁体   中英

Azure Table Storage inserting date properties incorrectly

Why do I have to create my entities' DateTime properties with DateTimeKind.Local ?

For example, I need to set the following property like so before inserting it into my table:

someObject.DatePerformed = new DateTime(2012, 11, 19, 3, 3, 3, DateTimeKind.Local);

If I do not specify DateTimeKind then it is saved incorrectly (2 hours are added). This happens on both my local storage emulator and on my Azure account. I am running the client application from my local machine at GMT +2 hours.

This is not an Azure issue, this is how DateTime works. You need to specify the time zone for the object DateTime .

To avoid issues when users are in different time zones, I recommend storing everything in UTC and then resolving it to local time at runtime.

Otherwise you are heading down a path of issues when manually converting timezones.

Azure runs on UTC time. If you are using DateTime.Now, use DateTime.AddHours().

var utcOffset = 2;
var dateValue = new DateTime(2009, 3, 1, 12, 0, 0);
var localTime = dateValue.AddHours(utcOffset);

or...

var localValue = DateTime.Now.AddHours(utcOffset);

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