Search code examples
azuredatetimestoragemilliseconds

How to force Azure Storage Table to save DateTime in specified format


I am saving a DateTime value into Azure Table Storage. The DateTime is being parsed from unix milliseconds format like so:

DateTimeOffset.FromUnixTimeMilliseconds(milliseconds).UtcDateTime;

When I look into the table via storage explorer the DateTimes have variable length. See Image

I was trying find out how to force the table to store the date in the following format but I wasn't able to find a way:

"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'"

Is there a way to force the desired format? Thanks


Solution

  • I was trying find out how to force the table to store the date in the following format but I wasn't able to find a way: "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'" Is there a way to force the desired format? Thanks

    This is not possible to force on Azure Table Storage. You can apply this type of logic on application code level.

    Like application level you can set.

    internal class Program
        {
            static string storageconn = "DefaultEndpointsProtocol=https;AccountName=storageaccount87819573y6;AccountKey=xxxxx;EndpointSuffix=core.windows.net";
    
            static string table = "Datetimetable";
            static string partitionkey = "Debasis Saha";
            static string rowKey = "userC";
    
            static void Main(string[] args)
            {
             Microsoft.Azure.Cosmos.Table.CloudStorageAccount storageAcc = Microsoft.Azure.Cosmos.Table.CloudStorageAccount.Parse(storageconn);
                Microsoft.Azure.Cosmos.Table.CloudTableClient tblclient = storageAcc.CreateCloudTableClient(new TableClientConfiguration());
                Microsoft.Azure.Cosmos.Table.CloudTable table1 = tblclient.GetTableReference(table);
                 
                Console.ReadKey();
    
                string datetime = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff");
                Console.WriteLine(datetime);
                Console.ReadKey();
    
            }
    

    Output :- you can pass this datetime to Azure Table Storage. enter image description here