Link to home
Start Free TrialLog in
Avatar of auldh
auldh

asked on

Convert string to a specific DateTime format.

Convert string date to DateTime.

My string is in this format (yyyy.mm.dd.hh.mm.ss) 2022.01.11.00.00.55.

I want to convert it into a "DateTime" SQL column.

I tried to find help on Internet but can't find an answer or a way to present my question.

The DateTime value should look like this (yyyy-mm-dd hh:mm:ss) 2022-01-11 00:00:55.

How do I do this in C#?

Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

string strDate = "2022.01.11.00.00.55.";
DateTime dt = DateTime.ParseExact(strDate, "yyyy.MM.dd.hh.mm.ss.",null);
System.Console.WriteLine(dt);
]

Open in new window

dt is a DATETIME not a string anymore

11-Jan.-2022 12:00:55 AM

string strDate = "2022.01.11.00.00.55.";
DateTime dt = DateTime.ParseExact(strDate, "yyyy.MM.dd.hh.mm.ss.",null);
System.Console.WriteLine(dt);
string date_str = dt.ToString("yyyy-MM-dd HH:mm:ss");
Console.WriteLine(date_str);

Open in new window



11-Jan.-2022 12:00:55 AM
2022-01-11 00:00:55
Avatar of auldh
auldh

ASKER

David Johnson, CD, Thank you for your quick response.
Looking for "DateTime dt" to be "2022-01-11 00:00:55".
Inputting the string "2022.01.11.00.00.55" to be converted to a DateTime variable of "yyyy-mm-dd hh:mm:ss" format, {2022-01-10 00:00:55}.
The SQL schema for the "TSTAMP" is (datetime, null) but using Europe format not US.

ASKER CERTIFIED SOLUTION
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of auldh

ASKER

Thank you David Johnson,
It seems that without changing the local PC Date Time property I may not see the desire result.
I will make this closed/solution.

Thanks again.