Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime.Parse American Date Format C#

Probably a simple question -

I'm reading in data from a number of files.

My problem is, that when I'm reading in the date from an american file, I parse it like so:

DateSold = DateTime.Parse(t.Date) 

This parses the string t.Date into a date format, however it formats the american date to a european date, e.g.

If the date is in the file as 03/01/2011, it is read as the 3rd of January, 2011, when it should be the 1st of March 2011.

Is there a way of doing this so that it formats to the european date?

like image 537
109221793 Avatar asked May 09 '11 14:05

109221793


1 Answers

var dt = DateTime.ParseExact(t.Date, "MM/dd/yyyy", CultureInfo.InvariantCulture); 

The DateTime itself has no formatting, it is only when you convert it to or from a string that the format is relevant.

To view your date with American format, you pass the format to the ToString method

string americanFormat = dt.ToString("MM/dd/yyyy"); 
like image 128
Patrick McDonald Avatar answered Sep 22 '22 20:09

Patrick McDonald