none
Parse "ISO 8601 BASIC" string RRS feed

  • Question

  • Hi,

    How can I parse an "ISO 8601 BASIC" string or an "extended" string or a normal string using a single method?

    Basically, 2020-02-03T00:00:00 is an "extended" string and "20200203T000000" is a "basic" string.

    Also, where's the best place to ask questions about .NET Core?

    I saw that in .NET Core 3.1, System.Text.Json, it's supposed to support "ISO 8601:-2019".

    So I made below little simple program.  It works with the dashes; throws an error without.  So Really the question is multiple questions: how do I parse Dates and Times without Dashes or Colons in .NET or .NET Core; and where do we ask questions about .NET Core.

            private class MyType
            {
                public DateTime D { get; set; }
            }

            static void Main(string[] args)
            {
                string text = @"{""D"":""2020-02-03""}";
                var thing = JsonSerializer.Deserialize<MyType>(text);
                Console.WriteLine(thing.D.ToString());
            }

    Monday, February 3, 2020 9:50 PM

Answers

  • Something like this:

    public DateTime? DecodeISO8601(string input)
    {
    	DateTime? result = null;
    	DateTime temp;
    
    	if (DateTime.TryParseExact(input.Replace(":", String.Empty).Replace("-", ""), "yyyyMMddTHHmmss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out temp))
    	{
    		result = temp;
    	}
    	return result;
    }

    If it cannot be parse, null is returned.

    In your Json container class, you may declare it as "string D {get; set;}" with a read-only auxiliary property "DateTime? DDate {get {return DecodeISO8601(this.D);}}

    ======

    If you want to make sure only string exactly matched these two formats, you may do this:

    public DateTime? DecodeISO8601(string input)
    {
    	DateTime? result = null;
    	DateTime temp;
    
    	if ((DateTime.TryParseExact(input, "yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out temp))
    		|| (DateTime.TryParseExact(input, "yyyyMMddTHHmmss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out temp)))
    	{
    		result = temp;
    	}
    	return result;
    }

    In C#, OR operator are "shortcut-evaluated" *(i.e.: If this left hand side is evaluated to true, the right hand side will not be evalated. Right hand side will only be evaluated if left hand side is false), so you can be sure that the first part won't be overwrited by second part if the input string is in the basic format.




    • Edited by cheong00Editor Tuesday, February 4, 2020 1:19 AM
    • Marked as answer by tballard Tuesday, February 11, 2020 8:06 PM
    Tuesday, February 4, 2020 1:01 AM
    Answerer

All replies

  • Something like this:

    public DateTime? DecodeISO8601(string input)
    {
    	DateTime? result = null;
    	DateTime temp;
    
    	if (DateTime.TryParseExact(input.Replace(":", String.Empty).Replace("-", ""), "yyyyMMddTHHmmss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out temp))
    	{
    		result = temp;
    	}
    	return result;
    }

    If it cannot be parse, null is returned.

    In your Json container class, you may declare it as "string D {get; set;}" with a read-only auxiliary property "DateTime? DDate {get {return DecodeISO8601(this.D);}}

    ======

    If you want to make sure only string exactly matched these two formats, you may do this:

    public DateTime? DecodeISO8601(string input)
    {
    	DateTime? result = null;
    	DateTime temp;
    
    	if ((DateTime.TryParseExact(input, "yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out temp))
    		|| (DateTime.TryParseExact(input, "yyyyMMddTHHmmss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out temp)))
    	{
    		result = temp;
    	}
    	return result;
    }

    In C#, OR operator are "shortcut-evaluated" *(i.e.: If this left hand side is evaluated to true, the right hand side will not be evalated. Right hand side will only be evaluated if left hand side is false), so you can be sure that the first part won't be overwrited by second part if the input string is in the basic format.




    • Edited by cheong00Editor Tuesday, February 4, 2020 1:19 AM
    • Marked as answer by tballard Tuesday, February 11, 2020 8:06 PM
    Tuesday, February 4, 2020 1:01 AM
    Answerer
  • Hi tballard,

    Thank you for posting here.

    So far, there is no dedicated .net core forum.

    Many people have mentioned this. 

    I will report it, and if there is new progress, I will inform you immediately.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, February 4, 2020 7:29 AM
  • Historically, since .NET Core 1.X and 2.X only support Console and Web applications, those questions are usually redirected to asp.net forums if web related, and answered here or .NET Framework library forums otherwise.

    I suspect with .NET 5 coming that will merge .NET Core and full .NET Framework versions, the forum admins may not create a new forum specific for .NET Core.

    Tuesday, February 4, 2020 7:43 AM
    Answerer