Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading Dates correctly regardless of format

Tags:

c#

datetime

Is there a way to read date time correctly regardless of date format (e.g. en-AU and en-US) in C# 4.0 or whatever framework version?

Below is two different date formats (top row is Australian format, bottom row is US format):

Timestamp,Windows User,Computer,Product,Version,Id,User,Action 
5/04/2011 9:14:16 AM,terryg,KETTSAM51,powerfulSoftware,3.0,0,Garry Terry,Lock
5/1/2011 12:38:22 PM,weanern,KETTSAM51,powerfulSoftware,3.0,1,Admin,Lock

What I want is to be able to read the dates, and process them all as Australian formatted dates.

like image 905
jw33 Avatar asked Aug 08 '11 01:08

jw33


2 Answers

string australiaCultureString = "en-AU";

CultureInfo australiaCulture = new CultureInfo(australiaCultureString);

To read as Australian culture:

DateTime time = DateTime.Parse(inputString, australiaCulture);

To represents Australian date time when call ToString()

string timeString = time.ToString(australiaCulture);
like image 94
Jalal Said Avatar answered Nov 09 '22 19:11

Jalal Said


I don't see a way you could do it, for example, how should this parse:

08/01/2010

That's a valid date in both US and AU formats, so there will always be ambiguity unless you give the parser an extra hint.

like image 26
Evan Trimboli Avatar answered Nov 09 '22 20:11

Evan Trimboli