How to convert time string into 24 hrs format

Hello Guys,
This is my string: “16/05/2018 08:19”
i am using (Date.ParseExact( str,“dd/MM/yyyy HH:mm”,system.Globalization.DateTimeFormatInfo.InvariantInfo)) .tostring(“yyyy-MM-dd HH:mm:ss”)

getting output: 2018-05-16 08:19:00
Expected output: 2018-05-16 20:19:00

what should be changed in above formula

1 Like

@smita.mobifly

Nothing to change. Already its printing 24 HR format only.

If you want to test it then try below:

This is my string: “16/05/2018 20:19”

Datetime.parseexact(str,“dd/MM/yyyy HH:mm”,system.globalization.cultureinfo.invariantculture).tostring(“dd-MM-yyyy hh:mm")

here, HH - 24 Hr format
hh - 12 Hr format

Hi,
why i am getting this error “String was not recognized as a valid datetime”
while passing hardcoded string its not showing any error.
message also showing correct time and date.

@smita.mobifly
buddy we need am or pm to get this in 24 hour format as 08:19 can be taken as both morning and night…but the compiler takes as morning

So try like this we will get the value, else you were doing great
Datetime.ParseExact(“16/05/2018 08:19 PM”,“dd/MM/yyyy hh:mm tt”,new system.Globalization.CultureInfo(“en-US”)).ToString(“yyyy-MM-dd HH:mm:ss”)

we will get answer like this

2018-05-16 20:19:00

Cheer buddy

3 Likes

Is that working buddy… @smita.mobifly

Cheers

@smita.mobifly

From where you are getting data ?

First print the value and see the format once.

Hi @Palaniyappan,

if i am passing hardcoded string like [Datetime.ParseExact(“16/05/2018 08:19 PM”,“dd/MM/yyyy hh:mm tt”,New system.Globalization.CultureInfo(“en-US”)).ToString(“yyyy-MM-dd HH:mm:ss”)]its working perfectly,
but if im passing [Datetime.ParseExact(generatedDate.tostring,“dd/MM/yyyy hh:mm tt”,New system.Globalization.CultureInfo(“en-US”)).ToString(“yyyy-MM-dd HH:mm:ss”)

its throwing error

1 Like

hi @lakshman,
type of variable is string.

Hey @smita.mobifly

Because your input date time string does not have suffix AM or PM and you are using it in parsing like “dd/MM/yyyy hh:mm tt” so it is not able to find the same formate to parse so you will get error if you will pass time with suffix mentioned it will not throw error.

Regards…!!
Aksh

1 Like

Buddy @smita.mobifly,

mention the AM or PM along the string variable buddy, that would work

Cheers

Keep going

thanks, its working…:slight_smile:

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