2013-03-11 69 views
1

我試圖將一些日期時間值作爲字符串從某些Javascript代碼傳遞到VB.net日期時間對象。Javascript日期轉換爲VB.net日期時間

這是林試圖轉換

星期四2012年9月27日14點21分42秒GMT + 0100(BST)

這裏是我到目前爲止,但它是真的努力轉換此日期字符串

Public Function TryParseDate(dDate As String) As Date 

    Dim enUK As New CultureInfo("en-GB") 
    Dim Converted_Date As Nullable(Of Date) = Nothing 
    Dim Temp_Date As Date 
    Dim formats() As String = {"ddd MMM d yyyy HH:mm:ss GMTzzz (BST)", _ 
           "ddd MMM d yyyy HH:mm:ss GMTzzz", _ 
           "ddd MMM d yyyy HH:mm:ss UTCzzz"} 

    ' Ensure no leading or trailing spaces exist 
    dDate = dDate.Trim(" ") 

    ' Attempt standard conversion and if successful, return the date 
    If Date.TryParse(dDate, Temp_Date) Then 
     Converted_Date = Temp_Date 
    Else 
     Converted_Date = Nothing 
    End If 

    ' Standard date parsing function has failed, try some other formats 
    If IsNothing(Converted_Date) Then 
     If Date.TryParseExact(dDate, formats, enUK, DateTimeStyles.None, Temp_Date) Then 
      Converted_Date = Temp_Date 
     Else 
      Converted_Date = Nothing 
     End If 
    End If 

    ' Conversion has failed 
    Return Converted_Date 

End Function 

TryParse和TryParseExact函數都返回false,指示轉換失敗。有誰知道發生了什麼事?或者更好的是,有一些代碼將成功轉換日期時間字符串。 有誰知道爲什麼這不起作用,並且

+0

如果你有Javascript代碼的控制,你可以使用'foo.toISOString( )'製作一個符合ISO 8601的字符串,這很容易製作成DateTime對象。 – 2013-03-11 11:31:03

回答

2

您正在使用錯誤的格式字符串。這裏的例子在F#中,但你應該得到的總體思路:-)

open System 
open System.Globalization 

let main argv = 
    let date = "Thu Sep 27 2012 14:21:42 GMT+0100 (BST)" 
    let dateparsed = DateTime.ParseExact(date, "ddd MMM dd yyyy HH:mm:ss 'GMT'zzzz '(BST)'", CultureInfo.InvariantCulture) 
    printfn "%A" dateparsed 
    0 

希望對大家有所幫助

MZ

+0

可愛的,謝謝你......曾經工作過。我想知道單引號,但不是100%確定它們的確切用途。 – JohnHenry 2013-03-11 13:28:35