none
DateTime Parsing RRS feed

  • Question

  • Imports System.Globalization
    Imports System.Threading
    
    Module Module1
        Sub Main()
    
            Dim s() As String = {"5 Jan 2009", "5 Jan, 2009", "Jan 5, 2019", "Jan 5 2019", "2019 05 Jan"}
    
            For Each item As String In s
    
                Try
                    Console.WriteLine($"{item} parsed to {DateTime.Parse(item)} under {CultureInfo.CurrentCulture}")
                Catch ex As Exception
                    Console.WriteLine("Unable to parse {0}", item)
                End Try
    
            Next
            Console.ReadLine()
        End Sub
    End Module

    I find that all the values in the array are getting parsed correctly. The current culture is english-India. However if I understand correctly then only those strings get parsed to date correctly which conform to the culture under which the parsing is happening. Now the long date format in the current culture is as shown below:

    My questions are as follows:
    1. MMMM stands for the full name of the month. None of the inputs follow this format. So why does it still get parsed correctly? Should it not give me an error because the input format does not conform to the format of the current culture?

    2. Also the comma does not make a difference. Same with the position of the year/month/day...seems like any order is working fine. Contrast this with the absence of comma and the presence of a defined format for dates in the culture. So then, How to find out what variations of the current/relevant culture date format are allowed for the parsing to happen smoothly?


    Sougata Ghosh


    • Edited by sougata12 Monday, August 27, 2018 12:29 PM
    Monday, August 27, 2018 12:28 PM

All replies

  • Hi,

    Learn about DateTime.ParseExact

    code:

    Imports System.Globalization
    Imports System.Threading
    
    Module Module1
    
        Sub Main()
            Dim s() As String = {"5 Jan 2009", "5 Jan, 2009", "1, 5, 2019", "Jan 5 2019", "2019 05 Jan", "20031223"}
    
            For Each item As String In s
    
                Try
                    Console.WriteLine($"{item} parsed to {DateTime.ParseExact(item, "yyyyMMdd", CultureInfo.CurrentCulture)} under {CultureInfo.CurrentCulture}")
                Catch ex As Exception
                    Console.WriteLine("Unable to parse {0}", item)
                End Try
    
            Next
            Console.ReadLine()
        End Sub
    
    End Module

    Best Regards,

    Alex


    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, August 28, 2018 2:13 AM
  • Hi,

    Thanks for the link but This was not my question. I will rephrase, if that adds to the clarity:

    We know that if we do not supply a datestye element explicitly to a parse method, then by default the whitespaces style is passed which allows leading/trailing white spaces to be parsed by the PARSE function. In the code block what we see is that the "," (comma) is automatically getting parsed implying that some default datestyle element must be getting passed which allows this character. Infact I find that if I replace the comma with "/" or "-" the parsing still works but if i use an "*" it does not. In other words, some datestyle element must be getting passed which allows "," "/" "-" but disallows the "*". Similarly, he position of the year/month/day does not seem to matter. And it is this point that I want to understand. 

    So my question is: 
    Is there a list where I can find all the variations that are allowed for a given culture for a PARSE method to function correctly? please note that I am not referring to PARSE.EXACT

    Regards,

    Sougata 


    Sougata Ghosh

    Tuesday, August 28, 2018 6:00 AM
  • Hi,

    https://blogs.msdn.microsoft.com/kathykam/2006/09/29/net-format-string-102-datetime-format-string/

    Best Regards,

    Alex


    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.

    Wednesday, August 29, 2018 1:45 AM
  • Hi,

    Don't think this answers my question. Have posted a detailed query the discussion section. You may refer to that since the question is very clearly explained there. But still thank you for taking out time to respond to my queries.

    Regards,

    Sougata


    Sougata Ghosh

    Sunday, September 2, 2018 10:37 AM