How to achieve this format?

Hi Team,

Please help me to achieve this…

String Variable = 1/30/2023, 09:45 AM to Output String Variable = 01/30/2023, 9:45 AM

after comma (,) space is only one time in output variable.

Thanks,
Ritesh

@Ritesh_Burman

This is how you can convert

DateTime.ParseEact(InputDate,"M/d/yyyy, hh:mm tt",System.Globalization.CultureInfo.InvariantCulture).ToString("MM/dd/yyyy. h:m tt")

This first one is input format and second one is the output format

Hope this helps

Cheers

Try this ,

String input= “1/30/2023, 09:45 AM”;
DateTime dateTime = DateTime.ParseExact(input, “M/dd/yyyy, hh:mm tt”, CultureInfo.InvariantCulture);
String output = dateTime.ToString(“MM/dd/yyyy, h:mm tt”);

Hi @Ritesh_Burman

Try this one

DateTime.ParseExact(InputDate,{"M/d/yyyy, hh:mm tt"},System.Globalization.CultureInfo.InvariantCulture,System.Globalization.DateTimeStyles.None).ToString("MM/dd/yyyy. h:m tt")

Regards
Sudharsan

I don’t understand what you want @Ritesh_Burman, i can help you but please explain better the case, what you need ?

Hi @Anil_G ,

Its giving me an Error like String was not recognized as Valid Date Time. What to do?

Regards,
Ritesh

Hi @Sudharsan_Ka ,

Thanks for helping. Same Error as above i mentioned.

Regards,
Ritesh

image

Hi can you print the input date and show that here ? @Ritesh_Burman

Use your comma as a delimiter first. It’s probably being read as a string because of it.

Then, format your date as a date.
Format your time as a time.
Reassemble your string with the comma.

@Ritesh_Burman

If there are two spaces then add two spaces…input format should exactly match woth the input value you send as string

I gave based on what you pasted here

Cheers

Hi @Sudharsan_Ka

That Error issue gets resolved now but output is not comming as AM or PM is comming as 01/30/2023, 10:31:00. Please help

Regards,
Ritesh

Hi @Sudharsan_Ka ,

Its Done Thanks.

Regards,
Ritesh

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