Javascript 日期转换为 VB.net DateTime

标签 javascript vb.net datetime

我试图将作为字符串传递的日期时间值从一些 Javascript 代码转换为 VB.net 日期时间对象。

这就是我要转换的内容

Thu 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,表示转换失败。有谁知道发生了什么事?或者更好的是,有一些代码可以成功转换日期时间字符串。 有谁知道为什么这不起作用,

最佳答案

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

关于Javascript 日期转换为 VB.net DateTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15337365/

相关文章:

python - 将日期函数应用于列以提取日期属性

sql - 将日期时间转换为 Unix 时间戳

javascript - 使用一个 AJAX html 结果填充多个 div 标签

javascript - 我对 shuffle 数组函数的设置和调用哪里出了问题?

javascript - ASP.NET 控件的 GetElementById 不断返回 'null'

.net - 在VB asp.net 2.0中对数据表的行进行分组

javascript - 如何在 JavaScript 中将用户字段设置为 SystemUserId?

c# - 使用具有父子关系 VB 或 C# 的 Web 服务

windows - 将 VB.Net 应用程序转换为服务

C# 日期时间 : Conversion for different time zones