将字符串日期“ddMMyyyy”转换为随机系统休眠日期 vb.net

问题描述 投票:0回答:1

我的场景:

我有一个随机日期,格式为“ddMMyyyy”。我想检查这个随机日期和今天之间的日差。

我知道我可以使用 DateDiff(DateInterval.Day, Date1 作为日期, Date 2 作为日期)

我的问题是我不知道哪种可能是系统的短日期格式,以便将字符串格式的日期转换为系统日期格式。

下面是我的带有注释的代码(有错误)。

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))

我最大的问题是如何将 2 位数字日/月/年转换为系统的格式化字符串。 我需要这样的 Convert.ToDateTime(10).ToString("MMM") 并将输出设置为“Oct”(或者如果选择希腊区域“Οκτ”并使用任何其他语言),或 Convert.ToDateTime( 10).ToString("MM") 输出为 10。

有什么建议吗?

vb.net datetime dateformatter
1个回答
0
投票

首先,日期应作为日期而不是字符串存储在数据库中。

鉴于上述问题,请不要想太多。

    '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
© www.soinside.com 2019 - 2024. All rights reserved.