Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Convert yyyymmdd to datetime

Tags:

c#

datetime

I'm trying to convert the UTC Time on Server that located overseas to local time e.g. Australia. Could anyone please help me to achieve this

Thanks.

like image 373
Nil Pun Avatar asked Nov 17 '25 14:11

Nil Pun


2 Answers

DateTime.ParseExact(s, "yyyyMMdd").ToLocalTime()

The MM needs to be capital, because mm means minutes, not months.

like image 73
agent-j Avatar answered Nov 20 '25 03:11

agent-j


Use DateTime.Parse().

If the input string does not have the timezone offset included (for example, 03/01/2009 05:42:00 -5:00, Sat, 01 Nov 2008 19:35:00 GMT or 2008-11-01T19:35:00.0000000-07:00), then ensure that the DateTme.Kind is "Utc", then call DateTime.ToLocal() to convert it to the local time zone.

like image 44
foxy Avatar answered Nov 20 '25 03:11

foxy