Reputation: 6653
I have a datetime value that I want displaying as a string in particular format, at the moment I am trying this
lastUpdate = DateTime.ParseExact(tmpDt.ToString(), "d/M/YYYY",
CultureInfo.InvariantCulture).ToString();
The error i'm getting is FormatException
Upvotes: 0
Views: 78
Reputation: 1464
why not just use ToString() with a format specifier
DateTime time = DateTime.Now;
string format = "MMM ddd d HH:mm yyyy";
Console.WriteLine(time.ToString(format));
Upvotes: 0
Reputation: 50712
Actually
tmpDt.ToString("dd/MM/YYYY",CultureInfo.InvariantCulture)
will do the same
Upvotes: 1
Reputation: 39898
You get the FormatException because you use a DateTime.ToString() and then expect it to be in " d/M/YYYY" for parsing it.
If you want to display your DateTime in a certain format you should use the overloads for DateTime.ToString().
Have a look at MSDN how to use this.
Upvotes: 0