简体   繁体   中英

DateTime format with Entity Framework

Can someone explain how it works? I need compare a DateTime in SQL Server with an user input.

Here an example of what I'm looking for:

DateTime[] dates = { DateTime.Now, DateTime.UtcNow };
Console.WriteLine(dates.Where(x => x < DateTime.Now));    // I need to know if they are the same without converting them to strings. 
Console.Read();

I'm using Entity Framework and in the entity class Dates , the attributes are DateTime and I need to compare that with a user's input, and the only solution I found was the CreateDate function.

As you know, I cannot compare the POST value directly with a Date attribute. Example:

db.dates.Where(x => x.StartDate < "2012-02-13 00:00:00.000")    // error, cannot compare DateTime with string

So, I tried converting the value to DateTime:

DateTime start_date = Convert.ToDateTime("2012-02-13 00:00:00.000");
db.dates.Where(x => x.StartDate < start_date )    // PROBLEM!!

Why? Well, the output of start_date is:

2012-02-13 12:00:00 am

But in the database, the end and start date are as follows:

2012-02-13 00:00:00.000    // all have zeros at the end

How can I compare the date when the input date is in 2012-02-13 00:00:00.000 format?

I don't care what to use, I just need the same format and the same data type.

i just need the same format

No, you don't. You need the most appropriate data type, which is DateTime (or maybe DateTimeOffset ) and you need to forget about the string format.

"2012-02-13 12:00:00 am" is just a different textual representation of the value which can also be represented as "2012-02-13 00:00:00.000". Both represent midnight on February 13th 2012. So long as your database field is an appropriate DateTime type, it shouldn't matter at all. You really don't need to worry about format.

You may need to worry about whether the values are in UTC or local time, but that's a separate matter.

(I'd personally use DateTime.Parse or DateTime.ParseExact rather than Convert.ToDateTime , but that's a slightly different matter.)

When you work with the DateTime, you first need to figure if you want to use the date and the time when comparing. If you want to include the time, then you need to you the Date.Now to initialize your variable. If you don't want to include the time, just compare the date portion of the data, then you need to use the Date.Today .

  • The Date.Now will always include the current time when the function was call.
  • The Date.Today will always set the time at 00:00:00 so it won't interfere when you compare the date.

The first thing you need to check in you code is to make sure you only use the Date.Today.

Now when I look at your example, I don't see any problem.

So, i tried convert the value to DateTime

DateTime star_date = Convert.ToDateTime("2012-02-13 00:00:00.000");

And lets go, lets compare again:

db.dates.Where(x=>x.StartDate < start_date )//PROBLEM!!

Why? Well, the output of start_date IS:

2012-02-13 12:00:00 am

When you compare x.StartDate < start_date, it is normal that you don't have any data because "2012-02-13 12:00:00" is equal to "2012-02-13 00:00:00.000". Since your dates are equal, it normal that your query return no data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM