2

The CosmosDB database I am using suggests I store DateTime in a string.

Working with Dates in Azure Cosmos DB

So I am trying to do this:

string DateAndTime = DateTime.Now.ToString()

but it's not giving me the correct format. Is there a method to do this and to convert it to this exact format: YYYY-MM-DDThh:mm:ss.sssZ ?

Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66
Alan2
  • 23,493
  • 79
  • 256
  • 450
  • What client are you using? – Oliver Apr 04 '20 at 08:55
  • duplicates:[DateTime in following format: 2016-01-13T11:11:11Z](https://stackoverflow.com/q/34925479/995714), [date format yyyy-MM-ddTHH:mm:ssZ](https://stackoverflow.com/q/1728404/995714), [How can I format DateTime to web UTC format?](https://stackoverflow.com/q/1820915/995714), [C# DateTime to “YYYYMMDDHHMMSS” format](https://stackoverflow.com/q/3025361/995714) – phuclv Apr 04 '20 at 09:07
  • 1
    Does this answer your question? [Given a DateTime object, how do I get an ISO 8601 date in string format?](https://stackoverflow.com/questions/114983/given-a-datetime-object-how-do-i-get-an-iso-8601-date-in-string-format) – phuclv Apr 04 '20 at 09:07
  • 1
    I've added the `azure-cosmosdb` tag since this question relates to it. – RoadRunner Apr 04 '20 at 09:11

2 Answers2

3

If you're not already using the DocumentClient, you could use this to interface with your collection.

Using the DocumentClient means that you can define your document types as you wish, without having to manually convert fields to string. For example, you could use the following type to create/query documents out of the collection:

public class CosmosDocument
{
    [JsonProperty("id")]
    public string Id { get; set; }

    public DateTime TimeStamp { get; set; }

}

The DocumentClient will then serialize/deserialize/model-bind the data, including DateTime values for you.

The serialization is configurable if you need to make any changes to the default settings.

EDIT

As noted in the comments, CosmosClient is the newer version that provides the same functionality described above.

Oliver
  • 8,794
  • 2
  • 40
  • 60
2

According to Custom date and time format strings, you should use something like that

string DateAndTime = DateTime.Now.ToString("yyyy-MM-ddThh:mm:ss.sssz");

It will give you the following string 2020-04-04T11:19:06.06+3

yyyy means four-digit year (YYYY format doesn't exist), dd represents the day of the month (from 01 to 31). z is used for offset between your local time zone and UTC

The capital Z literal in string indicates that datetime is in UTC format (according to ISO 8601 standard). To get it literally in a result string you may use DateTime.UtcNow instead of DateTime.Now with Z literal or K specifier at the end

string DateAndTime = DateTime.UtcNow.ToString("yyyy-MM-ddThh:mm:ss.sssK");

It gives you 2020-04-04T08:49:22.22Z (current datetime in UTC format). K specifier will add Z literal for UTC dates and offset (in zzz format) for local dates

Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66