narrow default width wide
colour style colour style colour style colour style

Determining date format/regional settings in .Net

I've looked for ways to do this for the longest time and I must say I was surprised that no one had a good way. All of the solutions I found were extremely complicated and didn't work all of the time.

Basically, my problem was that I had some date string that was formatted in a specific way (yyyymmddHHMMSS.mmmmmm) and I needed to be able to work with that date. I needed to convert that to a valid date variable type so I could do some date math (it was just a string data type). That would be easy but I need it to work internationally so I can't just parse the string and do a:

strDate = mm & "-" & dd & "-" & yyyy
 

If I do that on a system that has its regional settings set to something that expects dd-mm-yyyy I will be working with the wrong date, right? I've had this idea in my head for a while but just hadn't gotten around to testing it. It seems to work great though so I figured I would share it.

Basically, I am just converting a specific date that uses the WORD for the month to a string. Then, I just do some string processing to figure out whether the first 2 characters are for the month or for the day.

Obviously, first I have to get each individual part out:

Dim yyyy As String : yyyy = WMITime.Substring(0, 4)
Dim mm As String : mm = Mid(WMITime, 5, 2) 'Month
Dim dd As String : dd = Mid(WMITime, 7, 2) 'Day
Dim hh As String : hh = Mid(WMITime, 9, 2) 'hour
Dim mn As String : mn = Mid(WMITime, 11, 2) 'minutes
Dim ss As String : ss = Mid(WMITime, 13, 2) 'seconds

Here's the good stuff!

Try
 Dim dtmTestDate As Date : dtmTestDate = CDate("January 5, 2000")
 Dim strTestDate As String : strTestDate = dtmTestDate.ToShortDateString
 WriteLogEntry(vbTab & "Testdate: " & strTestDate, 1)
Select Case True
 Case strTestDate.Substring(0, 2) = "05" Or strTestDate.Substring(0, 1) = "5" ' Non-US
  strDateFormat = "NonUS"
 Case strTestDate.Substring(0, 2) = "01" Or strTestDate.Substring(0, 1) = "1" ' US
  strDateFormat = "US"
 Case Else
  strDateFormat = "UNKNOWN"
End Select
Catch ex As Exception
 Call ErrorHandler("ERROR: Cannot determine date format settings", Err, ex)
End Try
 

I needed to work with the dates multiple times in a loop of code somewhere else. That's why I didn't just set my dates once in the code above. I just figured out what the date format was so I could do what I needed with it in my loop.

Select Case strDateFormat
 Case "US"
  strDate = mm & "-" & dd & "-" & yyyy
 Case "NonUS"
  strDate = dd & "-" & mm & "-" & yyyy
 Case Else
End Select
 

Then, I combine that with the time (the format doesn't change for this if you have different regional settings--at least as far as I know)

Dim strTime As String = hh & ":" & mn & ":" & ss
CDate(strDate & " " & strTime)
 

Ta-da! I can now get the correct date regardless of the regional settings that it is running on. This means I don't have to maintain two separate versions depending on where the software will be used.