2

I am trying to compare date with format I require but it always fails that

FormatException: String was not recognized as a valid DateTime.

I am using correct format but what may be the issue?

My Sample Date format: "2018/04/13 18:56:02"


string format = "yyyy/MM/dd HH:mm:ss"; //date should always be in this format as per my requirement

Trail 1

_context.LogEntities.
.Where(p => (p.timestamp) > DateTime.Now.AddDays(-1))

Trail 2

_context.LogEntities.
.Where(p => (DateTime.ParseExact(p.timestamp, format, CultureInfo.InvariantCulture)) > DateTime.Now.AddDays(-1))

Trail 3

_context.LogEntities
.Where(p => Convert.ToDateTime(DateTime.ParseExact(p.timestamp, format, CultureInfo.InvariantCulture)) > DateTime.Now.AddDays(-1))
Kurkula
  • 6,386
  • 27
  • 127
  • 202
  • 2
    "I am using correct format but what may be the issue?" You are not using the correct format. The Exception is unambigiously clear about that. Show us the input string and we might be able to spot in wich culture it is actually formated. Rather then in what you asumed it is formated as. – Christopher Apr 13 '18 at 22:09
  • my sample date format is "2018/04/13 18:56:02" – Kurkula Apr 13 '18 at 22:12
  • 1
    If you want to know EXACTLY what is happening in this case, remove the linq statement and put your steps into a loop. Then you can debug line-by-line to see exactly what part is failing. Once you fix the error, put your linq statement back in. – user7396598 Apr 13 '18 at 22:13
  • Useful info. Bad part is lambda expressions has debugging issues in these cases. – Kurkula Apr 13 '18 at 22:15
  • 2
    What is the _exact_ value of `p.timestamp` when it fails? – D Stanley Apr 13 '18 at 22:15
  • I am converting to linq query for debugging. Thanks a ton for everyones time. @stanley as per you suggestion, I am looking in to data issues also while debugging. – Kurkula Apr 13 '18 at 22:25

1 Answers1

3

If your timestamp is in the specified format, you can create a DateTime object out of it like this : Parse string to DateTime in C#

then, you can compare two DateTime by simply using comparisons operators <, <=, ==, >, >=

So, your try 2 should work.

Please double check this attempt, because there is no reason you should have this error message in this case.

As suggested in comments, you might try to rewrite your code so it's more easily debuggable.

If you can find a way to perform separately the conversion with DateTime.Parse, in a simple loop for instance, that would allow you to examine more easily the different values and how and why the exception is raised.

Pac0
  • 21,465
  • 8
  • 65
  • 74
  • A good general advice. Do not keep a datetime around as string. However in this case there is a excpetion during the parsing, so I am not sure how much it would help. Still just having the parsing moved to a single place would propably aid debugging massively. If nothing else, you can skip using LINQ for that step. A simple loop should do. – Christopher Apr 13 '18 at 22:17
  • I completely agree, separating the conversion should be a kind of reflex here for debugging. – Pac0 Apr 13 '18 at 22:23