-3
DateTime.Now => 04.01.2019 06:20:51 AM

but I want it to be

04.01.2019 18:20:51.

I want to get date without am any to string format

var events = myEntity.Select(d => EntityFunctions.DiffHours(d.StartDate, DateTime.Now) <= 3 &&
      EntityFunctions.DiffHours(d.StartDate, DateTime.Now) >= 0).Select(d => new
      {
          d.Id,
          d.CalendarId,
          d.Title,
          d.StartDate,
          d.Calendar.Name
      }).ToList();
  • Have you taken a look at the custom format string options?: https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings – David Jan 04 '19 at 15:26
  • In debug mode you see date time in system date format. In code you can reformat when you convert it to string. `string myStringDate = DateTime.Now.ToString("dd.MM.yyy HH:mm:ss")` – Renatas M. Jan 04 '19 at 15:26
  • DateTime.Now.ToString("s") or DateTime.Now.ToString("dd-MM-yyyy") or whatever else you can think of. Have a look at https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings – Clark Kent Jan 04 '19 at 15:27
  • I could not convert DateTime.Now.ToString("dd.MM.yyy HH:mm:ss") in this query – user2814672 Jan 04 '19 at 15:34
  • @user2814672: Why not? Is it trying to convert that to a LINQ to Entities expression? In that case you wouldn't do the conversion in the query itself, you're just querying data here. You'd format the string when *displaying* the data. – David Jan 04 '19 at 15:38
  • Possible duplicate of [C# DateTime to "YYYYMMDDHHMMSS" format](https://stackoverflow.com/questions/3025361/c-sharp-datetime-to-yyyymmddhhmmss-format) – Ziv Weissman Jan 04 '19 at 15:40
  • @David StartDate, DateTime.Now comes with am so query run falsely.Start date=>2019-01-04 7:25:00 DateTime.Now=>2019-01-04 6:25:00 .when I substract,the result is 0 but I have one record – user2814672 Jan 04 '19 at 15:45
  • @user2814672: Do you mean you want to pass a value *to* your query as a custom format string? If that's the case then it's not really clear what you're trying to do. `EntityFunctions.DiffHours()` expects `DateTime` (or `TimeSpan`) values, not strings. If this is what you're trying to do then it's starting to sound like you're chasing the wrong problem based on an incorrect assumption somewhere else. – David Jan 04 '19 at 15:47

1 Answers1

0
    var from = DateTime.Now.AddHours(-3);
    var to= DateTime.Now;
    var events = myEntity.Where(d => d.StartDate >= from && d.StartDate <= to) 
                         .Select(d => new
                             {
                                d.Id,
                                d.CalendarId,
                                d.Title,
                                d.StartDate,
                                d.Calendar.Name
                             }).ToList();
pasha goroshko
  • 199
  • 2
  • 13