3

What is the best and fastest way to convert a DateTime to this format?

2015-03-26T18:02:58.145798Z

Currently I receive a date from a server and I'm able to parse it and convert the date in to DateTime and the ToString() output is something like this:

26/03/2015 18:02:58

For converting the date I'm using this line of code:

var parsedDate = DateTime.Parse("2015-03-26T18:02:58.145798Z", CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind);

What is the best way to convert parsedDate back to the original format?

EDIT: I want to convert the DateTime to this format 2015-03-26T18:02:58.145798Z as string

default
  • 11,485
  • 9
  • 66
  • 102
frenk91
  • 919
  • 1
  • 15
  • 30

3 Answers3

7

If you have a DateTime object you can convert it to a string with that particular format by using O as format specifier:

parsedDate.ToString("O")

or

parsedDate.ToUniversalTime().ToString("O") // if parsedDate is not UTC

returns "2015-03-26T18:02:58.1457980Z".


If the DateTimeKind of your DateTime object is not Utc then you won't get the Z extension at the end of the string according to ISO8601. In the example you provided the Z is present because DateTime.Parse will recognize it and return a DateTime in Utc. Should the Z be missing in the original string you parse you can still assume it's UTC by using ToUniversalTime() on the date time object.

Dirk
  • 10,668
  • 2
  • 35
  • 49
  • the uotput using ToString("O") is this 2015-04-23T08:03:39.0000000 it miss the Z at the end – frenk91 Apr 23 '15 at 08:31
  • 1
    @Default @frenk91 Because you use `DateTime.Now` which has `DateTimeKind.Local`. The parsed `DateTime` in the example has `DateTimeKind.Utc` in which case there will be a `Z` at the end. – Dirk Apr 23 '15 at 08:31
  • @Dirk you need to correct parsedDate.UtcNow, it will not work. Add DateTime.UtcNow as an alternative and explain why the Z is only on the end for UTC – Jaycee Apr 23 '15 at 08:38
  • @Jaycee there is no `parsedDate.UtcNow`, If he parses the string the way he says he will get a UTC time. – Dirk Apr 23 '15 at 08:42
  • @Dirk what I am saying is you are using the original DateTime variable and calling UtcNow on it, it is a static property! – Jaycee Apr 23 '15 at 08:44
  • 1
    @Default fixed it now. I checked for existance of the method but did not notice that is was static. My bad! – pyrocumulus Apr 23 '15 at 08:48
4

The answer is almost what @Dirk said:

parsedDate.ToString("O") is the line, but you have to convert the DateTime to UTC: that's how you get the "Z" at the end.

See https://msdn.microsoft.com/en-us/library/az4se3k1%28v=vs.110%29.aspx for more info.

Edit:

To convert a DateTime to UTC, use the ToUniversalTime() method.

Tamás Deme
  • 2,194
  • 2
  • 13
  • 31
0

The FASTEST way, known to me, is this:

        ///<summary>Format the date time value as a parsable ISO format: "2008-01-11T16:07:12Z".</summary>
    public static string ISO( this DateTime dt ) {
        var ca = new char[] {
            (char) ( dt.Year / 1000 % 10 + '0' ),
            (char) ( dt.Year / 100 % 10 + '0' ),
            (char) ( dt.Year / 10 % 10 + '0' ),
            (char) ( dt.Year % 10 + '0' ),
            '-',
            (char) ( dt.Month / 10 + '0' ),
            (char) ( dt.Month % 10 + '0' ),
            '-',
            (char) ( dt.Day / 10 + '0' ),
            (char) ( dt.Day % 10 + '0' ),
            'T',
            (char) ( dt.Hour / 10 + '0' ),
            (char) ( dt.Hour % 10 + '0' ),
            ':',
            (char) ( dt.Minute / 10 + '0' ),
            (char) ( dt.Minute % 10 + '0' ),
            ':',
            (char) ( dt.Second / 10 + '0' ),
            (char) ( dt.Second % 10 + '0' ),
            'Z',
        };
        return new string( ca );
    }
cskwg
  • 790
  • 6
  • 13