Assign : String was not recognized as a valid DateTime

Hi ,

I was trying to get the time difference, Facing error as “String was not recognized as a valid DateTime.”

LastSuccessFinishedSyncTIME : “13-09-2021 12:40:00”

TimeDiff :

Convert.ToString( (DateTime.ParseExact(Now.ToString(“yyyy-MM-dd HH:mm:ss”), “yyyy-MM-dd HH:mm:ss”,System.Globalization.CultureInfo.InvariantCulture)-DateTime.ParseExact(LastSuccessFinishedSyncTIME, “yyyy-MM-dd HH:mm:ss”,System.Globalization.CultureInfo.InvariantCulture)).TotalHours)

Can someone please help in this ?

@supermanPunch @Srini84

TimeConvert.xaml (5.7 KB)

Hi @Mandava_Naresh

Please try this

Convert.ToString( (DateTime.ParseExact(Now.ToString(“dd-MM-yyyy HH:mm:ss”), “yyyy-MM-dd HH:mm:ss”,System.Globalization.CultureInfo.InvariantCulture)-DateTime.ParseExact(LastSuccessFinishedSyncTIME, “dd-MM-yyyy HH:mm:ss”,System.Globalization.CultureInfo.InvariantCulture)).TotalHours)

Thanks

Hi @prasath_S ,

Thanks for your time… I tried it but still no luck

Please try this updated one,

Convert.ToString((now-DateTime.ParseExact(LastSuccessFinishedSyncTIME, “dd-MM-yyyy HH:mm:ss”,System.Globalization.CultureInfo.InvariantCulture)).TotalHours)

Have tested it please check

Thanks

please find the corrected one
TimeConvert.xaml (5.9 KB)

Regards,
Arivu

2 Likes

not matching your pattern

yyyy-MM-dd HH:mm:ss
we do see in the pattern the year, Month, Day order
but in sample the day, Month Year order

Why are you converting Now to a string then back to a datetime? Now is already a datetime.

Hi @arivu96 ,

file which you shared is working but it failed again when I tried with different value “2021-09-13 15:40:00”

Thanks,
Naresh

Is LastSuccessFinishedSyncTIME a datetime? If not, it should be. Then you don’t have to do any conversions, just DateDiff with Now and LastSuccessFinishedSyncTIME.

LastSuccessFinishedSyncTIME a datetime? → I have to get this text from application and check the diff

DateDiff(“h”,Now(),DateTime.ParseExact(“09-12-2021 02:00:00”,“MM-dd-yyyy HH:mm:ss”,CultureInfo.CurrentCulture))

The format you provide to ParseExact is to tell it what’s what in the string. It’s an input format not an output format.

You’re trying to use System.Globalization.CultureInfo.InvariantCulture but it’s failing, probably because your system datetime format isn’t the same as the LastSuccessFinishedSyncTIME format.

Worked it out. Here you go.

DateDiff(“h”,Now(),DateTime.ParseExact(LastSuccessFinishedSyncTIME,“yyyy-MM-dd HH:mm:ss”,CultureInfo.CurrentCulture))

This gives 33 (hours). Change the “h” depending on which interval you want.

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