在 azure table 存储中存储 DateTime
Storing DateTime in azure table storage
我正在使用默认示例在 table 存储中存储日期时间值。其中一个字段计算如下
DateTime accMonth = new DateTime(DateTime.Now.Year,
DateTime.Now.Month, 1);
通常上面表示时间为00:00的日期。
然而,当我将其保存在 table 存储中时,我这次看到的是
2018-04-01T18:30:00.000Z
这让我觉得很奇怪!有人知道为什么吗?
您似乎希望将 DateTime 格式化为:
yyyy-MM-dd 00:00:00
如果是这样,您可以使用下面的代码来实现:
DateTime accMonth = new DateTime(DateTime.Now.Year,DateTime.Now.Month, 1);
string formatted = accMonth.ToLongTimeString();
double hour = DateTime.Now.Hour;
customer1.date = Convert.ToDateTime(formatted).AddHours(-hour+1);
完整代码如下:
private static void Main()
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
CloudTable table = tableClient.GetTableReference("people");
CustomerEntity customer1 = new CustomerEntity("Harp1", "Walter1");
DateTime accMonth = new DateTime(DateTime.Now.Year,DateTime.Now.Month, 1);
string formatted = accMonth.ToLongTimeString();
double hour = DateTime.Now.Hour;
customer1.date = Convert.ToDateTime(formatted).AddHours(-hour+1);
TableOperation insertOperation = TableOperation.Insert(customer1);
table.Execute(insertOperation);
}
public class CustomerEntity : TableEntity
{
public CustomerEntity(string lastName, string firstName)
{
this.PartitionKey = lastName;
this.RowKey = firstName;
}
public CustomerEntity() { }
public DateTime date { get; set; }
}
屏幕截图是:
您将获得不同的值,因为您正在创建具有本地时区的 date/time(印度是 GMT+5:30)。在 Azure 存储中,date/time 值保存为 UTC。
SDK 所做的是将此日期转换为 UTC,然后保存该日期。这就是为什么您看到 date/time 值比您设置的 date/time 值早 5:30 小时。
Edm.DateTime under Property Types
要解决此问题,请将 date/time 类型指定为 UTC。然后 SDK 不会做任何转换。所以你的代码将是:
var accMonth = new DateTime(DateTime.Now.Year,
DateTime.Now.Month, 1, 0, 0, 0, DateTimeKind.Utc);
我正在使用默认示例在 table 存储中存储日期时间值。其中一个字段计算如下
DateTime accMonth = new DateTime(DateTime.Now.Year,
DateTime.Now.Month, 1);
通常上面表示时间为00:00的日期。
然而,当我将其保存在 table 存储中时,我这次看到的是
2018-04-01T18:30:00.000Z
这让我觉得很奇怪!有人知道为什么吗?
您似乎希望将 DateTime 格式化为: yyyy-MM-dd 00:00:00
如果是这样,您可以使用下面的代码来实现:
DateTime accMonth = new DateTime(DateTime.Now.Year,DateTime.Now.Month, 1);
string formatted = accMonth.ToLongTimeString();
double hour = DateTime.Now.Hour;
customer1.date = Convert.ToDateTime(formatted).AddHours(-hour+1);
完整代码如下:
private static void Main()
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
CloudTable table = tableClient.GetTableReference("people");
CustomerEntity customer1 = new CustomerEntity("Harp1", "Walter1");
DateTime accMonth = new DateTime(DateTime.Now.Year,DateTime.Now.Month, 1);
string formatted = accMonth.ToLongTimeString();
double hour = DateTime.Now.Hour;
customer1.date = Convert.ToDateTime(formatted).AddHours(-hour+1);
TableOperation insertOperation = TableOperation.Insert(customer1);
table.Execute(insertOperation);
}
public class CustomerEntity : TableEntity
{
public CustomerEntity(string lastName, string firstName)
{
this.PartitionKey = lastName;
this.RowKey = firstName;
}
public CustomerEntity() { }
public DateTime date { get; set; }
}
屏幕截图是:
您将获得不同的值,因为您正在创建具有本地时区的 date/time(印度是 GMT+5:30)。在 Azure 存储中,date/time 值保存为 UTC。
SDK 所做的是将此日期转换为 UTC,然后保存该日期。这就是为什么您看到 date/time 值比您设置的 date/time 值早 5:30 小时。
Edm.DateTime under Property Types
要解决此问题,请将 date/time 类型指定为 UTC。然后 SDK 不会做任何转换。所以你的代码将是:
var accMonth = new DateTime(DateTime.Now.Year,
DateTime.Now.Month, 1, 0, 0, 0, DateTimeKind.Utc);