PARSING DATES in VISUAL BASIC: HOW THE PARSE FUNCTION BEHAVES FOR DIFFERENT DATE SEPARATORS?

  • Thread starter Thread starter sougata12
  • Start date Start date
S

sougata12

Guest
There are 4 cultures for which the observations are made. They are: en-US, en-GB, fr-FR, fi-FI. The culture currently set in the control panel is fi-FI. The following date separators are studied:
1. Comma (,)
2. Backslash (/)
3. Hyphen (-)

The study tries to understand how the PARSE function behaves when one or a combination of the above date separators are used. We know that by default the AllowWhiteSpaces style value is passed to a parse method. However from the observations mentioned below, it seems that this default style value also supports the presence of one of the date separators (individual or a combination) mentioned above.

The first major conclusions are:
1. Dates get successfully parsed when date separators (,) & (/) & (-) are used only once (without any consecutive repetition).

1321523.png

2. ONLY A SINGLE SYMBOL IS USED BUT IS REPEATED: When only a single symbol is used but is repeated then it is observed that the comma (“,”) can be used multiple times with or without spaces. But the other two symbols when used more than once as date separators give rise to a parsing error. The following 2 screenshots show the same:
1321524.png


1321525.png

3. SYMBOLS ARE USED IN COMBINATION: When used in combination, the comma can be used multiple times (with or without spaces). However the other two symbols (-) and (/) can be used only once. If these two symbols are used more than once the parsing operation fails. See the screenshot below:

1321526.png

So as we see from the above screenshots, clearly there are cases (individual or combination) when the comma, backslash or the hyphen gets accepted as a date-separator and can be parsed smoothly. The question I would like to pose is that there are there other symbols which also get accepted as valid date separators and hence can be parsed smoothly? Clearly, as seen from the below screenshot, not any random symbol is allowed as a date separator:

1321527.png

The code used to generate the above outputs is given below:

Imports System.Globalization
Imports System.Threading

Module Module1
Sub Main()
Dim dateStrings() As String = {"01,02,2015", "01/02/2015", "01-02-2015", "01,, 02, 2015", "01, 02,,2015",
"01// 02/2015", "01/02 //2015", "01--02-2015", "01-02--2015",
"01,,02/2015", "01,,02//2015", "01, ,02/2015", "01/ /02,2015",
"01,,02 / // /2015", "01-,02-/2015", "01- /02//,2015"}

Dim cultureNames() As String = {"en-US", "en-GB", "fr-FR", "fi-FI"}

For Each item As String In cultureNames
Console.WriteLine($"Short date format in {item} culture is: {CultureInfo.CreateSpecificCulture(item).
DateTimeFormat.ShortDatePattern}")
Console.WriteLine($"Date separator in {item} culture is: {CultureInfo.CreateSpecificCulture(item).
DateTimeFormat.DateSeparator}")
Next
Console.WriteLine()
Console.WriteLine($"{"Original",-30}{"Parsed",-30}{"Culture",-30}")
Console.WriteLine()
For Each item As String In dateStrings
For Each i As String In cultureNames
Try
Console.WriteLine($"{item,-30}{DateTime.Parse(item, CultureInfo.CreateSpecificCulture(i)),-30}{CultureInfo.CreateSpecificCulture(i),-30}")
Catch ex As Exception
Console.WriteLine($"{item,-30}{"Cannot parse",-30}{CultureInfo.CreateSpecificCulture(i),-30}")
End Try
Next
Console.WriteLine()
Next

Console.ReadLine()
End Sub
End Module


Request you to chip in with your thoughts on this.


Regards,
Sougata






Sougata Ghosh

Continue reading...
 
Back
Top