Format dateTime without toString

Hi Team,

We are getting dateTime column from excel with format dd/MM/yyyy HH:mm:ss.
We are trying to convert the date format as yyyy-MM-dd HH:mm:ss using DateTime.ParseExact.

Please see the below Example.

Datetime Date1 = 07/06/2023 10:00:00;
String Date2 = String.Format(“{0:yyyy-MM-dd HH:mm:ss}”,Date1);
DateTime Date3 = DateTime.ParseExact(Date2,“yyyy-MM-dd HH:mm:ss”,System.Globalization.DateTimeFormatInfo.InvariantInfo);

Output:
Date1: 07/06/2023 10:00:00
Date2: 2023-06-07 10:00:00
Date3: 07/06/2023 10:00:00

Please let us know how to convert the datetime into yyyy-MM-dd HH:mm:ss without ToString().

HI,

Is the code above not working ?

Regards,
Amit

Hi Amit,

Yes, its not working, we we are using DateTime.ParseExact but again date format is converting into dd/MM/yyyy HH:mm:ss.