Answered by:
get all records created today when date is formatted as UTC

Question
-
User-2051535749 posted
How can I get all the records that were created today when the createDate has UTC time stored in it?
example:
2020-01-30 13:00:30.33
I just want the record if it was created today (1-30-2020)
My where clause:
var x = db.SalesRecords .Where(x.CreatedDate == DateTime.Today)
Is there a way to pass to get only today's records no matter the format in the table?
Thursday, January 30, 2020 7:54 PM
Answers
-
User281315223 posted
If you only explicitly want to check for records for today, you'll need to make sure you ignore the time components, which you should be able to do via just accessing the Date property (which DateTime.Today already does):
var x = db.SalesRecords .Where(x.CreatedDate.Date == DateTime.Today)
Or simply just always check if the date was created today or later:
var x = db.SalesRecords .Where(x.CreatedDate >= DateTime.Today)
It's worth noting there's also a DateTime.UtcNow property that could be useful if you are strictly dealing with UTC dates across the board.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, January 30, 2020 8:29 PM -
User-2051535749 posted
Guess it would help if I saved the file instead of refreshing the page. It works now
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, January 30, 2020 9:08 PM
All replies
-
User281315223 posted
If you only explicitly want to check for records for today, you'll need to make sure you ignore the time components, which you should be able to do via just accessing the Date property (which DateTime.Today already does):
var x = db.SalesRecords .Where(x.CreatedDate.Date == DateTime.Today)
Or simply just always check if the date was created today or later:
var x = db.SalesRecords .Where(x.CreatedDate >= DateTime.Today)
It's worth noting there's also a DateTime.UtcNow property that could be useful if you are strictly dealing with UTC dates across the board.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, January 30, 2020 8:29 PM -
User475983607 posted
Try
DateTime Today = DateTime.Today; DateTime Tomorrow = DateTime.Today.AddDays(1); var x = db.SalesRecords .Where(x.CreatedDate >= Today & x.CreatedDate <= Tomorrow)
Thursday, January 30, 2020 8:32 PM -
User-2051535749 posted
When I run either one of these, I get no records, even though there are 5 in the table that were created today. I get an error message:
The specified type member 'date' is not supported by LINQ to Entities.
var x = db.SalesRecords .Where(x.CreatedDate.Date == DateTime.Today)
when I run in debug, it has DateTime.Today = 1/30/2020 12:00:00 AM., which I think is causing it to fail on me
the date is stored as: 2020-01-30 14:00:14.336 in the table
Thursday, January 30, 2020 8:41 PM -
User-2051535749 posted
Guess it would help if I saved the file instead of refreshing the page. It works now
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, January 30, 2020 9:08 PM