1

I've encountered a problem when trying to parse a data from the DB.

The data I have for example is: 2019-04-19T00:00:00.000Z

I'm trying to get it to the format of dd/MM/yyyy but I'm encountering the error:

String was not recognized as a valid DateTime.

Here's my code block

string x = DateTime.ParseExact("2019-04-19T00:00:00.000Z","'\"'yyyy-MM-dd'T'HH:mm:ss.fff'Z\"'", CultureInfo.InvariantCulture).ToString("dd/MM/yyyy");

Am I specifying the wrong format? Or are there any other ways of doings this?

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
Gashio Lee
  • 119
  • 1
  • 15
  • `DateTime.Parse("2019-04-19T00:00:00.000Z").ToString("dd/MM/yyyy")` – stuartd Apr 25 '19 at 09:45
  • 1
    Possible duplicate of [how to convert date with 'T' to/from string in C#](https://stackoverflow.com/questions/14252312/how-to-convert-date-with-t-to-from-string-in-c-sharp) – Sinatr Apr 25 '19 at 09:45

4 Answers4

1

Your format string should be "yyyy-MM-ddTHH:mm:ss.fffZ" instead for "'\"'yyyy-MM-dd'T'HH:mm:ss.fff'Z\"'" That is, the code should be like this example

string x = DateTime.ParseExact("2019-04-19T00:00:00.000Z","yyyy-MM-ddTHH:mm:ss.fffZ", CultureInfo.InvariantCulture).ToString("dd/MM/yyyy");
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
1
DateTime.ParseExact("\"2019-04-19T00:00:00.000Z\"", "'\"'yyyy-MM-dd'T'HH:mm:ss.fff'Z\"'", null).ToString("dd/MM/yyyy");

that will give you 19/04/2019

0

Check this:

string x = DateTime.ParseExact("2019-04-19T00:00:00.000Z", "yyyy-MM-dd'T'HH:mm:ss.fff'Z'", CultureInfo.InvariantCulture).ToString("dd//yyyy");
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
0

you can also just parse it -

var x = DateTime.Parse("2019-04-19T00:00:00.000Z",
     CultureInfo.InvariantCulture, 
     DateTimeStyles.RoundtripKind).ToString("dd/MM/yyyy");
NDJ
  • 5,189
  • 1
  • 18
  • 27