Converting String into DateTime format - "yyyy-MM-dd HH:mm:ss"

Hi All,

Im converting a string “MM/dd/yyyy HH:mm:ss” to a DateTime “yyyy:MM:dd HH:mm:ss” using: Datetime.ParseExact(str_datetime, “yyyy-MM-dd hh:mm:ss”,System.Globalization.CultureInfo.InvariantCulture)

I start of by converting the string to the new format: DateTime.ParseExact(str_datetime, “dd/MM/yyyy HH:mm:ss”, System.Globalization.CultureInfo.InvariantCulture).ToString(“yyyy-MM-dd HH:mm:ss”)

this works ok.

then I convert the new string into a DateTime. using: Datetime.ParseExact(str_datetime, “yyyy-MM-dd hh:mm:ss”,System.Globalization.CultureInfo.InvariantCulture)

this works but the format I get is: “MM/dd/yyyy HH:mm:ss”

can anyone tell me why this is? and what I need to change to get it right?

Hi @matt.guzek

Sonce your DataType is System.DateTime, this will take the default system format which is MM/dd/yyyy hh:mm:ss.

If you want to convert your string input into a specified required format then the datatype of the output variable will be System.String only. Since you are changing the format of your input into desired format.

Regards

Hi,

DateTime parsedDateTime = DateTime.ParseExact(formattedDateString, “yyyy-MM-dd HH:mm:ss”, CultureInfo.InvariantCulture);

use above things,

Regards,
Mohamed Farhan

@matt.guzek,

If you want to print the datetime in specific format then only it will matter what format you want.

If you have a datetime in DateTime Data type then it’s as per your system time format setting. You can use as you require anywhere.

Thanks,
Ashok :slight_smile: