Date time String to DateTime Assign Error

hi all,

I have a string of date time with format (yyyyMMdd hhmmss)
Eg: 20210121 143230

This string of date time was scrapped from a table row in a webpage, with initial format of a dot (.) in between the date and time.
Initial format scrapped: 20210121.143230
Format after removing the dot: 20210121 143230 - i removed the dot using Substring by the way.

I want to change the date time String into DateTime, to compare with current datetime, using this:
DateTime = DateTime.ParseExact(str_DateTime, “yyyyMMdd hhmmss”, nothing)

However, I got the error of ‘Assign: String was not recognized as a valid DateTime’.

I also have tried using:
DateTime = DateTime.ParseExact(str_DateTime, “yyyyMMdd hhmmss”, System.Globalization.CultureInfo.InvariantCulture)
And still got the same error as earlier.
Can anyone help on this? I have already check through tons of other topics but still cant find the solution.
Does the method of using Substring contributes to the error?
Please help.

welcome to the Forum
try HH instead of hh

Hey that works!
is there any reason why HH is working instead of hh? i thought that only applies for MM and mm?

@syezids - HH is 24 hrs format where as hh is 12 hours. Please check the different data time formats here

ahh I see, okay then.
Got a lot more to learn for me. Thanks for the additional reference :+1:

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.