[How to convert time string into 24 hrs format]

My String is dateError = “06/11/2021 7:00:18 PM”

Trying to convert it to 24 hrs Format using below;
Datetime.ParseExact(dateError,“dd/MM/yyyy hh:mm:ss tt”,new system.Globalization.CultureInfo(“en-US”)).ToString(“yyyy-MM-dd HH:mm:ss”)

But i am getting error as “String was not recognized as a valid DateTime.”

image

May I know what I did wrong here.

Hi!

Try using Datetime.ParseExact(dateError,“dd/MM/yyyy h:mm:ss tt”,new system.Globalization.CultureInfo(“en-US”)).ToString(“yyyy-MM-dd HH:mm:ss”)

Your date already has AM | PM, so no “hh” is required, just “h”

Hope it works for you!

h and hh does not has to anything with AM and PM.

  1. h → 12-hour clock hour (e.g. 4).

  2. hh → 12-hour clock, with a leading 0 (e.g. 06)

@spdhoke - Please check this…

image

Date.Parse(“06/11/2021 7:00:18 PM”).ToString(“MM/dd/yyyy HH:mm:ss”)

image

True, however a date won’t parse if you use 24 hour clock and AM | PM

24 hrs clock is different – that HH and H…User did not used that…Please check this…i have used hh and it still worked when there is a leading zeros…

Since the hour is 7 (not 07) we should not use hh.

1 Like

That is not the intended output…please check the requirement again…

So change the format at the end to what you want.

Date.Parse(“06/11/2021 7:00:18 PM”).ToString(“yyyy-MM-dd HH:mm:ss”)

Hey @spdhoke!

I keep this overview I got from one of the courses (from way back). Personally I find it very helpful:


Might be helpful to you (and others) that encounter this issue.

Good luck!

Yes, This is working. Thanks :slight_smile:
But may i know what was the issue with the one i was using.

Thanks :slight_smile:

You had:
Datetime.ParseExact(dateError,“dd/MM/yyyy hh:mm:ss tt”,New system.Globalization.CultureInfo(“en-US”)).ToString(“yyyy-MM-dd HH:mm:ss”)

So either use one of these:
Date.Parse(dateError).ToString(“MM/dd/yyyy HH:mm:ss”) + Environment.NewLine +
Datetime.ParseExact(dateError,“dd/MM/yyyy h:mm:ss tt”,New system.Globalization.CultureInfo(“en-US”)).ToString(“yyyy-MM-dd HH:mm:ss”)
afbeelding

Pretty much what @prasath17 said. hh:mm:ss tt must be h:mm:ss tt

Cheers!

Hello,

I’m having the same issue. I’m reading an outlook email with the current date.

Here is the code:

EmailMessage.Where(Function(x) DateTime.ParseExact(x.Headers(“Date”), “MM/dd/yyyy HH:mm:ss”, Nothing).Date.Equals(DateTime.Now.Date))

I’m getting a “String was not recognized as a valid DateTime.” error
Here is the system Date & Time setting
e1

Best Regards,
Vrushali

Please open up a new forum post with your query. We will be able to assist you.

1 Like

Hello Again,

That day it worked for the below senario;
strStartDate - “06/17/2021 3:30:00 AM”
Convert.ToDateTime(Date.Parse(strStartDate).ToString(“MM/dd/yyyy HH:mm:ss”))

but now i have value which i am getting from excel;
strExcelValue = “17/06/2021”
Convert.ToDateTime(Date.Parse(“17/06/2021”).ToString(“dd/MM/yyyy”))

Its again giving error as “Assign: String was not recognized as a valid DateTime.”

Can any1 help.

Are you using “Preserve Format” in the read range??

Remember, Date.parse takes your local setting(Time). Say if your system date format is MM/dd/yyyy and if you using dd/MM/yyyy to parse it, it will fail…Please check below…

In this case, you have to use datetime.parseexact…

Yes, the system format is MM/dd/yyyy.
But then i have the value from excel as text which is dd/MM/yyyy.

so i need to check if today’s date falls in between date1 and date2.
date1,date2 are taken from excel as text.
Ex.: date1 = “14/06/2021” and date2 = “20/06/2021”.
then need to check if today’s date falls between these 2 dates or not.

@ElectricBoogie @postwick

1 Like