0

How to compare two datetime values?

For example as per below condition, I have FDatetimes value is string coming (i.e 2021-06-21 17:00:30Z) from database and SDatetimes value should be Datetime.UtcNow. So my requirement is both left and right side always compare with datetime instead of string.

Also I have tried to use Convert.ToDateTime(FDatetimes). But there is one issue here. Whenever I have used the above function, That time the time is getting changed to 2021-06-21 10:00:30Z. My main aim is to compare both field value as DateTime instead of String. Please help in VB.Net

If FDatetimes < SDatetimes Then
  'My logic
End If
GSerg
  • 76,472
  • 17
  • 159
  • 346
kamal
  • 25
  • 7
  • You have to convert to a DateTime object using DateTime.Parse(string) – jdweng Jun 23 '21 at 12:48
  • 1
    Why would you be storing dates and times as text in the first place? You're now trying to fix a problem of your own creation – jmcilhinney Jun 23 '21 at 12:54
  • It will be great if you can provide any example. – kamal Jun 23 '21 at 12:59
  • It probably converts the time given as UTC (this is what Z means) to your local time. Try this `utcNow = DateTime.Parse( nowStr, null, DateTimeStyles.AdjustToUniversal );` from [this answer](https://stackoverflow.com/a/833192/880990). (Use `Nothing` instead of `null` in VB and dismiss the semicolon.) – Olivier Jacot-Descombes Jun 23 '21 at 13:05
  • I have used the above code for left side(FDatetimes ) condition to show datetime value which is converting like this (#6/23/2021 11:59:33 AM#). To compare with right hand side condition, How to use code for this(SDatetimes ). SDatetimes should be current datetime as per utc time. – kamal Jun 23 '21 at 13:28
  • There is no point in creating a string containing `Datetime.UtcNow`. Use the utc time directly. `If DateTime.Parse( FDatetimes, Nothing, DateTimeStyles.AdjustToUniversal ) < Datetime.UtcNow Then`. Avoid storing date/times as string if possible. – Olivier Jacot-Descombes Jun 23 '21 at 13:32
  • Thank You for your quick reply. I have made code like this. Please verify. Is it correct. Dim fdatetimes= DateTime.Parse(FDatetimes, Nothing, DateTimeStyles.AdjustToUniversal) and Dim currDateTime = DateTime.UtcNow. Then my condition is If fdatetimes< currDateTime Then – kamal Jun 23 '21 at 13:57
  • @kamal, Looks good. Test it. – Olivier Jacot-Descombes Jun 23 '21 at 15:13
  • Thank You. I have tested . It works fine as per your code. Then i thought the below code will be more releevant, but not sure. Then i have changed my code as per below code . That also works fine. Do you have any suggestion on this below code.Dim FDT As DateTime = DateTime.ParseExact(FDatetimes, "yyyy-MM-dd HH:mm:ssZ", Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.AdjustToUniversal Or Globalization.DateTimeStyles.AssumeUniversal) – kamal Jun 24 '21 at 14:17

1 Answers1

1

So to resolve DST issues I did this (which I ran at 9:20AM local, 14:20 UTC on 23 Jun 2021)

Dim SDatetimes As DateTime = DateTime.UtcNow
Dim FDatetimes As String = "2021-06-23 15:20:30Z"

Dim FDT As DateTime = DateTime.ParseExact(FDatetimes,
                          "yyyy-MM-dd HH:mm:ssZ",
                           Globalization.CultureInfo.InvariantCulture,
                           Globalization.DateTimeStyles.AdjustToUniversal Or
                           Globalization.DateTimeStyles.AssumeUniversal)

Debug.WriteLine(SDatetimes.ToString)
Debug.WriteLine(FDT.ToString)
If FDT < SDatetimes Then
    'My logic
    Stop
End If
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
dbasnett
  • 11,334
  • 2
  • 25
  • 33
  • This looks good to me. I am not sure whether `ParseExact` accepts a specific time zone or whether it wants a `K` meaning "Time zone information". see: [Custom date and time format strings](https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings). – Olivier Jacot-Descombes Jun 23 '21 at 15:12