Search code examples
vb.netdatetimedateformatter

Convert a string date "ddMMyyyy" to random System Dormat Date vb.net


My scenario:

I have a random date in string formatted like "ddMMyyyy". (I know that is wrong to save data as string, please don't judge i get the data from a 3rd-party app). And i want to check the day difference between this random date and today.

I know that i can find the days using DateDiff(DateInterval.Day, Date1 as Date, Date 2 as Date)

My problem is that i dont know which may be the system's short date format, in order to convert the string formatted date to system date format.

Below is my code with comments (with error).

Dim Date_String As String = "01102024" 'I get this from a DB and  cant change this format
Dim ShortDateFormat As String = System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat.ShortDatePattern() 'Here i get the system's date format as string

'Below i try to find the seperator character (i know it may be more that these).
    Dim SplitChar As String
    If ShortDateFormat.Contains("/") Then
                    SplitChar = "/"
                Else
                    SplitChar = "-"
                End If

        Dim DateConvertedToSystemDate As String = ""

'I use the Array to seperate the d M y parts
        Dim ArrayShortDateFormat() As String = Split(ShortDateFormat, SplitChar)
'For each array item 
'I check if is day,month or Year
'I try to convert my 2 chars strings to system formats 
        For i As Integer = 0 To ArrayShortDateFormat.Length - 1
            If ArrayShortDateFormat(i).Contains("d") Then
                DateConvertedToSystemDate = DateConvertedToSystemDate + Convert.ToDateTime(Date_String.Substring(0, 2)).ToString(ArrayShortDateFormat(i)) 'This don't work  because of "Convert.ToDateTime(Date_String.Substring(0, 2)).ToString(ArrayShortDateFormat(i))"
            ElseIf ArrayShortDateFormat(i).Contains("M") Then
                DateConvertedToSystemDate = DateConvertedToSystemDate + Convert.ToDateTime(Date_String.Substring(2, 2)).ToString(ArrayShortDateFormat(i)) 'This don't work  because of "Convert.ToDateTime(Date_String.Substring(2, 2)).ToString(ArrayShortDateFormat(i))"
            ElseIf ArrayShortDateFormat(i).Contains("y") Then
                DateConvertedToSystemDate = DateConvertedToSystemDate + Convert.ToDateTime(Date_String.Substring(4, 2)).ToString(ArrayShortDateFormat(i)) 'This don't work  because of "Convert.ToDateTime(Date_String.Substring(4, 2)).ToString(ArrayShortDateFormat(i))"
            Else
                MsgBox("Error")
            End If
'Add systems seperator 
            DateConvertedToSystemDate = DateConvertedToSystemDate & SplitChar
        Next

'remove the last inserted char seperator
        DateConvertedToSystemDate = DateConvertedToSystemDate.Substring(0, DateConvertedToSystemDate.Length - 1)

'find the difference in days
    Dim days As Long = DateDiff(DateInterval.Day, Convert.ToDateTime(Today.ToShortDateString), Convert.ToDateTime(DateConvertedToSystemDate))

My biggest problem is how can i convert my 2 digit day/month/year to system's formated string. I need something like this Convert.ToDateTime(10).ToString("MMM") and the output to be "Oct"(or if for example greek region is selected "Οκτ" and in any other language), or Convert.ToDateTime(10).ToString("MM") and the output to be 10.

Any suggestions?


Solution

  • First of all dates should be stored in the DB as dates, not strings.

    Given the problem as stated don't overthink this.

        'Given this as a format that can't be changed
        '  ddMMyyyy
        Dim Date_String As String = "01102024" 'I get this from a DB and  cant change this format
    
        Dim provider As Globalization.CultureInfo = Globalization.CultureInfo.InvariantCulture
    
        Dim DateFromString As Date
        DateFromString = Date.ParseExact(Date_String, "ddMMyyyy", provider) 'convert string to date
    
        Dim diff As TimeSpan = DateFromString - Date.Now 'the difference