Javascript日期转换到VB.net DateTime

Javascript date conversion to VB.net DateTime

本文关键字:net DateTime VB 日期 转换 Javascript      更新时间:2023-09-26

我试图转换一个datetime值作为一个字符串传递从一些Javascript代码到VB.net datetime对象。

这是我想转换的

星期四Sep 27 2012 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,表示转换失败。有人知道是怎么回事吗?或者更好的是,有一些代码可以成功地转换datetime字符串。有人知道为什么这不起作用吗?

您使用了错误的格式字符串。这里有一个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