Date Comparision is not working with CosmosDB Date Property in .Ner Core

Shreekumar Suggamad 146 Reputation points
2022-08-03T14:39:18.78+00:00

I have many documents as below in the CosmosDB container

{  
    "id": "62ea57a5e85b97356ea8e8b4",  
    "snapshotId": "62ea57a55238bbda2b06d446",  
    "statusTypeCode": "available",  
    "transactionId": "2507280",  
    "stockRecordID": "2520929",  
    "sku": 7806200,  
     "packSize": 12,  
    "quantityOfUnits": 107,  
    "unitOfMeasure": "each",  
    "svfCreatedDate": "2022-08-01",  
    "createdDate": "2022-08-01",  
    "lastModifiedDate": "2022-08-01",  
    "_rid": "YvdMAPTHOwsBAAAAAAAAAA==",  
    "_self": "dbs/YvdMAA==/colls/YvdMAPTHOws=/docs/YvdMAPTHOwsBAAAAAAAAAA==/",  
    "_etag": "\"00000000-0000-0000-a73e-e2629c6b01d8\"",  
    "_attachments": "attachments/",  
    "_ts": 1659534153  
}  

And I'm trying to get documents by Date like this :

var allDocs = mycontainer.Where(d =>  
     d.CreatedDate == DateTime.UtcNow.AddDays(-2).Date  
).ToList();  

Date comparison is not working here. I tried updating the Document with different Date Formats, Removing the Time Part. Am I missing anything? Please advice.

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,902 questions
0 comments No comments
{count} votes

Accepted answer
  1. Hasan Savran 331 Reputation points MVP
    2022-08-03T19:40:26.823+00:00

    DateTime needs to be saved in UTC format for any range filters to work.
    JSON does not have a datatype to save date or datetime. So all are getting saved as string data type.
    I wrote about this in my blog.
    https://h-savran.blogspot.com/2019/11/how-to-handle-datetime-in-cosmos-db.html

    I hope this helps.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.