Joseph Le Brech
Joseph Le Brech

Reputation: 6653

DateParsing a string converted from a Date

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

Answers (4)

thedrs
thedrs

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

Arsen Mkrtchyan
Arsen Mkrtchyan

Reputation: 50712

Actually

tmpDt.ToString("dd/MM/YYYY",CultureInfo.InvariantCulture)

will do the same

Upvotes: 1

Otiel
Otiel

Reputation: 18743

Try:

myDateTimeObj.ToString("d/M/yyyy");

Upvotes: 0

Wouter de Kort
Wouter de Kort

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

Related Questions