2015-02-24 69 views
1

如果我有一個包含此格式的日期字符串:(VB.net)的特殊日期格式的字符串通用日期(MM-DD-YYYY)

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 

我真的很感謝你的幫助,感謝提前

+0

也閱讀了本*仔細*之前,你的解決方案張貼要求:[提問] – Plutonix 2015-02-24 01:44:26

回答

2

它有點complicat如果日期字符串格式不同,則編輯。不過你可以試試這個:

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 
+0

它返回「 01-01-0001「 – 2015-02-24 16:20:15

+0

@交通大師對於哪個字符串? – Baby 2015-02-24 16:30:12

+0

所有的字符串 – 2015-02-24 20:08:01