question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

system.text.json + azure functios + cosmos db

See original GitHub issue

I’m not sure if it’s really an issue or normal behavior. But I have the following situations:

public DateTimeOffset Date1 { get; set; } = DateTimeOffset.Now;
public DateTimeOffset Date2 { get; set; } = DateTimeOffset.UtcNow;
public DateTime Date3 { get; set; } = DateTime.Now;
public DateTime Date4 { get; set; } = DateTime.UtcNow;

And it is saving as follows in the cosmos:

"date1": "2021-02-01T12:28:55.845-03:00",
"date2": "2021-02-01T15:28:55.962+00:00",
"date3": "2021-02-01T12:28:55.963-03:00",
"date4": "2021-02-01T15:28:55.963Z",

I’m trying to use the date functions of the cosmos (DateTimeDiff), but it only accepts the last format.

Shouldn’t the cosmos save exclusively in the last format, which is what it accepts?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
drma-devcommented, Feb 1, 2021

Thank you. Anyway, I’m using the example that works: public DateTime Date4 {get; set; } = DateTime.UtcNow

1reaction
ealsurcommented, Feb 1, 2021

Shouldn’t the cosmos save exclusively in the last format, which is what it accepts?

What do you mean by accept? Are you getting errors with the other formats?

This is part of Newtonsoft.Json serialization of datetimes: https://www.newtonsoft.com/json/help/html/DatesInJSON.htm

You could configure the default settings to use a particular format, decorate the class to use a custom converter. From the SDK perspective, this is a string field and it is sent on the wire to the backend. There is no validation that should happen on the SDK. If the backend can store the data, then it’s valid.

Is the problem that you are afterwards executing queries? If so, there is a doc talking about it: https://docs.microsoft.com/en-us/azure/cosmos-db/working-with-dates#storing-datetimes

But this is not a requirement, you could be storing dates in any format because they might be later on consumed by a reader that does not execute queries or something like that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

CosmosDBAsyncCollector uses Newtonsoft ...
CosmosDBAsyncCollector uses Newtonsoft instead of System.Text.Json (.NET Core 3.1 and Azure Function runtime version ~3) #629.
Read more >
Under the hood of the new Azure Functions extension for ...
Azure.DocumentDB.Core package). This SDK used Newtonsoft.Json as serialization engine. The new extension, which uses the Azure Cosmos DB SDK V3 ...
Read more >
System.Text.Json.JsonPropertyName not working for ...
This article explains how to fix an annoying issue with Microsoft's SDK for CosmosDb v3 – it comes with a Newtonsoft.Json dependency, that...
Read more >
Connect Azure Functions to Azure Cosmos DB using ...
This article shows you how to use Visual Studio Code to connect Azure Cosmos DB to the function you created in the previous...
Read more >
Azure Functions v4 serialize to Cosmos DB in camel case
How can I tell Cosmos SDK to write documents with camel case without using [JsonPropertyName] in all properties of the model? For instance...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found