DateTime Parsing

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

sougata12

Guest
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:

1309421.png

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

Continue reading...
 
Back
Top