DateParsing a string converted from a Date
-
13-02-2021 - |
Question
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
Solution
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.
OTHER TIPS
Actually
tmpDt.ToString("dd/MM/YYYY",CultureInfo.InvariantCulture)
will do the same
Try:
myDateTimeObj.ToString("d/M/yyyy");
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));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow