DateOnly and TimeOnly are the new members of .NET family, they are introduced in version 6. Developers have been asking to declare only a date or only a time without using the DateTime object for a long time. These two new functions will make the developer's life easy for sure. You can declare them easily but saving them to a database can be a challenge. Before we go into those details for saving them, Let's look at how to declare and use them in the code.
DateOnly(year, month, day)
TimeOnly(hour, min, sec, ms)
You are lucky if you have a NoSQL database. Since you don't need to worry about schemas in NoSQL. You can easily save DateOnly or TimeOnly to NoSQL databases.
I tried to declare a test class with DateOnly and TimeOnly properties and save it to Azure Cosmos DB. The result was surprisingly good. Cosmos DB SDK serialized the object as it is. I was expecting to see the result in a string. I was wondering how this would work with indexing.
Here is my Console code that creates an item in Azure Cosmos DB.
This is what data looks like in Azure Cosmos DB. The object is serialized as an object rather than a string. Saving date or time in this format gives me a lot of flexibility when it comes to querying the data.
No comments:
Post a Comment