Search code examples
pythonazureazure-table-storageazure-storage-account

Storing DateTime fields in Azure Table Storage


I'm trying to store an ISO formatted DateTime in my Azure Table Storage. But every time I try, it results in this String format: https://i.sstatic.net/kcrlN.png

This is a summary of the effective code I'm running:

import jsons
from azure.functions import Out

def main(parameterTableOutputJSON: Out[str]):
    row = {'ReadOn': '2021-04-28T09:35:26.123456Z'}
    parameterTableOutputJSON.set(jsons.dumps(row))

Of course my real code also has the PartitionKey and RowKey columns, but I can't show those here.

How can I insert the DateTime in the Azure Table Storage like this: 2021-04-28T09:35:26.123456Z
Instead of this: 04/28/2021 09:35:26

The Azure Table Storage docs tell me it supports ISO formatted date times...


Solution

  • I found the solution to my own problem.

    The jsons.dumps method strips the milliseconds by default. This results in a datetime that's formatted like this: 2021-04-28T09:35:26Z
    This format is not supported as a DateTime in Azure Table Storage.

    The problem is solved by writing the last line of my code like this:

    parameterTableOutputJSON.set(jsons.dumps(row, strip_microseconds=False))