0

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

2
  • Then it's an incorrect format. Since you haven't posted the string you send into it, it's impossible for us to determine why. Also, why are you calling .ToString() on tmpDt? What type is tmpDt? Commented Oct 26, 2011 at 9:01
  • FormatException means your date was in the wrong format. Please show your date format in tmpDt. Commented Oct 26, 2011 at 9:01

4 Answers 4

1

Actually

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

will do the same

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

Try:

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

Comments

0

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)); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.