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#?

SQLC

Avatar of undefined
Last Comment
auldh

8/22/2022 - Mon
David Johnson, CD

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
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
David Johnson, CD

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
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.
Your help has saved me hundreds of hours of internet surfing.
fblack61