Question

I am having a problem while converting

 2003-11-05T16:35:30Z  to "11/5/2013"

I tried below

 DateTime.ParseExact("2003-11-05T16:35:30Z", "dd/MM/yyyy", null)

but I am not getting the value I expect.

Was it helpful?

Solution

You want to do this:

DateTimeOffset.Parse("2003-11-05T16:35:30Z").ToString("dd/MM/yyyy")

What you're doing is parsing the data as if it were in the format dd/MM/yyyy - instead, you want to parse it as the universal format it is, and then convert it to a string with your format string.

DateTime.Parse will take a string and return a date time. To get a string back out, you simply use the ToString method.

OTHER TIPS

Try This:

string myDate = DateTime.ParseExact("2003-11-05T16:35:30Z", 
                               "yyyy-MM-ddTHH:mm:ssZ",null).ToString("M/d/yyyy");

When parsing you specify the format that you are parsing from, then you format the DateTime value using the new format:

string reformatted =
  DateTime.ParseExact("2003-11-05T16:35:30Z", "yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'", null)
  .ToString("dd/MM/yyyy");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top