none
ISO Datetime convert RRS feed

  • Question

  • Hi,

    I have to convert datetimes to ISO datetime in this format:

    ISO datetime, YYYY-MM-DDThh:mm:ss+TZD

    I tried this:

    string dateTime = DateTime.Now.ToString("YYYY-MM-DDThh:mm:ss+TZD", CultureInfo.InvariantCulture);

    But the output is this:

    YYYY-01-DDT11:24:08+TZD

    What is that mean? is this correct?

    Thanks in advance.

    Kbid 

    Wednesday, January 2, 2019 10:26 AM

Answers

  • Not sure if these are what you need.

    var dt = new DateTime(2019, 1, 2, 20, 45, 0, 70, DateTimeKind.Utc);
    var result = dt.ToString("o", CultureInfo.InvariantCulture);
    Console.WriteLine(result);

    Or

    public static class DateTimeIsoExtensions
    {
        private const string DateTimeFormat = "{0}-{1}-{2}T{3}:{4}:{5}Z";
        public static string ToIso8601Date(this DateTime date)
        {
            return string.Format(
                DateTimeFormat,
                date.Year,
                PadLeft(date.Month),
                PadLeft(date.Day),
                PadLeft(date.Hour),
                PadLeft(date.Minute),
                PadLeft(date.Second));
        }
        public static DateTime FromIso8601Date(this string date)
        {
            return DateTime.ParseExact(date.Replace("T", " "), "u", CultureInfo.InvariantCulture);
        }
    
        private static string PadLeft(int number)
        {
            if (number >= 10) return number.ToString(CultureInfo.InvariantCulture);
            return $"0{number}";
    
        }
    }

    Usage for extension

    var isoDate1 = DateTime.Now.ToIso8601Date();
    Console.WriteLine(isoDate1 + " Kind: " + DateTime.Now.Kind);
    
    var date1 = isoDate1.FromIso8601Date();
    Console.WriteLine(isoDate1 + " Kind: " + date1.Kind);
    
    var isoDate = DateTime.UtcNow.ToIso8601Date();
    Console.WriteLine(isoDate + " Kind: " + DateTime.UtcNow.Kind);
    
    var date = isoDate.FromIso8601Date();
    Console.WriteLine(isoDate + " Kind: " + date.Kind);


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by KBid Wednesday, January 2, 2019 3:41 PM
    Wednesday, January 2, 2019 11:19 AM
    Moderator
  • Check three variants:

    string dateTime1 = DateTime.Now.ToString( @"yyyy'-'MM'-'dd'T'HH':'mm':'ss''zzz", CultureInfo.InvariantCulture );
    string dateTime2 = DateTime.Now.ToUniversalTime().ToString( @"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'", CultureInfo.InvariantCulture );
    string dateTime3 = DateTime.Now.ToUniversalTime().ToString( "s", CultureInfo.InvariantCulture ) + 'Z';


    • Edited by Viorel_MVP Wednesday, January 2, 2019 11:26 AM
    • Marked as answer by KBid Wednesday, January 2, 2019 3:41 PM
    Wednesday, January 2, 2019 11:22 AM

All replies

  • Not sure if these are what you need.

    var dt = new DateTime(2019, 1, 2, 20, 45, 0, 70, DateTimeKind.Utc);
    var result = dt.ToString("o", CultureInfo.InvariantCulture);
    Console.WriteLine(result);

    Or

    public static class DateTimeIsoExtensions
    {
        private const string DateTimeFormat = "{0}-{1}-{2}T{3}:{4}:{5}Z";
        public static string ToIso8601Date(this DateTime date)
        {
            return string.Format(
                DateTimeFormat,
                date.Year,
                PadLeft(date.Month),
                PadLeft(date.Day),
                PadLeft(date.Hour),
                PadLeft(date.Minute),
                PadLeft(date.Second));
        }
        public static DateTime FromIso8601Date(this string date)
        {
            return DateTime.ParseExact(date.Replace("T", " "), "u", CultureInfo.InvariantCulture);
        }
    
        private static string PadLeft(int number)
        {
            if (number >= 10) return number.ToString(CultureInfo.InvariantCulture);
            return $"0{number}";
    
        }
    }

    Usage for extension

    var isoDate1 = DateTime.Now.ToIso8601Date();
    Console.WriteLine(isoDate1 + " Kind: " + DateTime.Now.Kind);
    
    var date1 = isoDate1.FromIso8601Date();
    Console.WriteLine(isoDate1 + " Kind: " + date1.Kind);
    
    var isoDate = DateTime.UtcNow.ToIso8601Date();
    Console.WriteLine(isoDate + " Kind: " + DateTime.UtcNow.Kind);
    
    var date = isoDate.FromIso8601Date();
    Console.WriteLine(isoDate + " Kind: " + date.Kind);


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by KBid Wednesday, January 2, 2019 3:41 PM
    Wednesday, January 2, 2019 11:19 AM
    Moderator
  • Check three variants:

    string dateTime1 = DateTime.Now.ToString( @"yyyy'-'MM'-'dd'T'HH':'mm':'ss''zzz", CultureInfo.InvariantCulture );
    string dateTime2 = DateTime.Now.ToUniversalTime().ToString( @"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'", CultureInfo.InvariantCulture );
    string dateTime3 = DateTime.Now.ToUniversalTime().ToString( "s", CultureInfo.InvariantCulture ) + 'Z';


    • Edited by Viorel_MVP Wednesday, January 2, 2019 11:26 AM
    • Marked as answer by KBid Wednesday, January 2, 2019 3:41 PM
    Wednesday, January 2, 2019 11:22 AM
  • > is this correct?

    Does it look correct to you?  What year does it say?

    I'm guessing you didn't check the documentation at all.  If you had, you would have seen that the year is specified as "yyyy" not "YYYY", the date is "dd" not "DD", and the time zone is "zzz", not "TZD".  Further, the documentation points to a "Formatting Utility" that lets you play with different formats and learn what they do.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Wednesday, January 2, 2019 7:22 PM