2013-03-11 71 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