我试图将作为字符串传递的日期时间值从一些 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/