(VB.net)特殊日期格式化字符串到通用日期(MM-DD-YYYY)

时间:2015-02-24 01:31:42

标签: vb.net date formatting

如果我有一个包含日期格式的字符串:

    Nov, 04 1983
    May 10th, 1988
    July 17 1979
    July 08, 1978
    January 03rd, 1990
    Jan 5th 1985
    Dec 8, 1988.
    August 5, 1969
    Aug., 28, 1983
    9th May,1978
    9th April 1976
    7th February 1983
    7th February 1983
    7july 1986
    6th Oct. 1986
    5th July 1982
    5th July 1973
    5th Jan, 1985
    5th Dec 1982.
    5th August 1987
    5th Aug, 1990
    3rd November 1982.
    3rd February,1982
    3rd December 1986
    31th May 1981
    31st of August 19876
    31st August 1990
    31st AUGUST 1987
    31st August 1978
    31'DEC 1978
    30th October 1986
    30th December 1978.
    30-06-1987
    30/07/1982
    2nd Sep. 1987
    2nd Sep 1989
    2nd July 1974
    2nd Dec. 1990.
    2nd Dec. 1983.
    2nd Dec. 1983.
    29-07-1986
    28-march-1987
    28/07/1986
    28 April, 1981
    27-07-1985
    27/01/1993
    26th May, 1988
    26th June 1981.
    26-DEC-1974
    25th NOV 1985
    25th June, 1976
    25th Dec 1985
    25-05-1985
    25/07/85
    25 Year & 28.09.1985.
    24th June 1987.
    24th July 1977
    -24th Dec 1977
    24th April 1989
    23rd March 1980
    23rd December, 1990
    22nd April 1984
    22nd- Apr-1989
    22.11.1989
    22 FEB 1990
    22 April 1988.
    21st September 1972
    21st June 1990
    21st Jan 1983
    21 August 1985
    20/08/1988
    20/08/1987
    20/02/1985
    19TH JUNE. 1986
    19th June, 1987
    19-08-1988
    18th June 1987
    18/03/1980
    17th September, 1975
    17th April 1985
    16th, March 1983
    16th May 1987
    16-October-1988
    16/11/1989
    16 / 06 / 1981
    15th June, 1979
    15-02-1989

我希望它将其转换为MM-DD-YYYY格式,在Vb.net中寻找解决方案 这是我正在使用的代码函数

    Public Function ParseDate(ByVal txt As String)
            If txt.Length > 20 Then
        Dim a() As String
        a = txt.Split("")
        If a.Length = 0 Then
            a = txt.Split(vbTab)
        End If
        If a.Length = 1 Then
            a = txt.Split("&")
            If a.Length > 1 Then
                txt = a(1).Trim()
                GoTo ok1
            End If
        End If
        If a.Length > 1 Then
            For Each ad As String In a
                If Len(ad.Trim()) >= 8 Then
                    txt = ad.Trim()
                    GoTo ok1
                End If
            Next
        End If
    End If
    ok1:
    txt = txt.Replace(":", " ").Trim()
    txt = txt.Replace(vbLf, " ").Trim()
    txt = txt.Replace(",", " ").Trim()
    txt = txt.Replace(vbTab, " ").Trim()

    Dim result As String = ""
    Dim mydate As New Date
    txt = Regex.Replace(txt.ToLower, "[\s+,.'`-]|(ust)|(st)|(rd)|(nd)|(th)", " ")
    Date.TryParse(txt, mydate)
    result = mydate.ToString("MM-dd-yyyy")

    Return result
 End Function

我非常感谢您的所有帮助,并提前感谢

1 个答案:

答案 0 :(得分:2)

如果日期字符串的格式不同,则有点复杂。但你可以试试这个:

Function FormatDateString(ByVal str As String) As String
    Dim result As String = ""
    Dim mydate As New Date
    str = Regex.Replace(str.ToLower, "[\s+,.'`-]|(ust)|(st)|(rd)|(nd)|(th)", " ")
    Date.TryParse(str, mydate)
    result = mydate.ToString("MM-dd-yyyy")
    Return result
End Function

示例:

Console.WriteLine(FormatDateString("January 03rd, 1990"))   
Console.WriteLine(FormatDateString("August., 21st, 1983"))
Console.WriteLine(FormatDateString(" - 13/08/1984-"))

输出:

01-03-1990
08-21-1983
08-13-1984