Convert Datetime to Date

Asked

Viewed 651 times

-2

I need to compare a date without the time, only ("dd/MM/yyyy") with the database with Linq, but I’m not able to transform the format DateTime("dd/MM/yyyy HH:mm:s") for Date("dd/MM/yyyy")?

  • 2

    where the code ?

  • You use Entity Framework?

  • Add more information, like the code. But I posted the answer and see if that’s it. Anything I update.

  • Related: https://docs.microsoft.com/en-us/dotnet/api/system.data.objects.entityfunctions?redirectedfrom=MSDN&view=netframework-4.7.2

  • Related: https://msdn.microsoft.com/en-us/library/system.data.entity.dbfunctions%28v=vs.113%29.aspx

1 answer

1

To do this, just do the following: take only the date of the Datetime object.

Example:

DateTime apenasData = DateTime.Now.Date;

it will put the hour as 0.

So, to use the Int, you can use:

 resultado = lista.Where(x => x.DataComparada.Date <= dataComparar.Date);
  • Depending on the version this works depending on does not work ... !!!

  • If he’s using Linq to Entities, really.

  • I just put it as an observation @Rodrigo !!!

  • But with the . Date it will compare this value: '24/10/2018 00:00:00', but I need it to take this: '24/10/2018'

  • From what I understood in the question the idea would be to compare. The date type does not exist in C#. When you put the hours as 0, you can compare normally. But if you want you can take it as a string, you know? Just converting data.Tostring("dd/MM/yyyy"). This code I posted in the reply converts the hours to 00:00 of the two comparable.

  • Got it, converting to string I know, the question was really whether it is possible to catch a Date type without the time("dd/MM/yyyy") but without converting to string, because when I am assembling my Linq, oddly enough, if I pass without the time, the consultation is almost 1 second faster

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.