I have a javascript AJAX call passing a javascript date object to an ASP.Net MVC controller. Here is the AJAX call:
var timestamp = new Date();
$.ajax({
url: '',
type: 'POST',
data: { username: username,
timestamp: timestamp,
hash: ''},
dataType: 'json',
(I have omitted some sensitive values, but the important bits are there).
The controller has an object for the parameters being passed which gets populated just fine.
Public Class AjaxParams
Public Property Username As String
Public Property Hash As String
Public Property Timestamp As String
End Class
And here is the controller declaration:
<HttpPost()> _
Public Function RetrieveSalt(params As AjaxParams)
When making a call to the controller, all of the parameters get correctly passed and populate the AjaxParams object.
However, later on in my code, I am trying to convert the passed javascript date from its string representation into a VB.net DateTime object. I created a function to do this which works ok for some dates but it simply is not robust enough to convert all necessary date formats and is failing on a number of dates.
Here is one of the failing date formats that must be converted:
Tue Sep 17 2013 07:01:36 GMT+0600 (ALMT)
Here is the function I created:
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 'GMT'zzzz '(BST)'", _
"ddd MMM d yyyy HH:mm:ss 'GMT'zzzz '(GMT)'", _
"ddd MMM d yyyy HH:mm:ss 'GMT'zzzz", _
"ddd MMM d yyyy HH:mm:ss 'UTC'zzzz"}
' Ensure no leading or trailing spaces exist
dDate = dDate.Trim(" ")
Select Case True
Case IsNumeric(dDate)
Dim Unix_Date As Long = Long.Parse(dDate)
Try
Dim newDate As Date = New DateTime(1970, 1, 1, 0, 0, 0, 0)
Converted_Date = newDate.AddMilliseconds(Unix_Date)
Catch ex As Exception
Converted_Date = Nothing
End Try
Case Else
Try
Converted_Date = JsonConvert.SerializeObject(dDate)
Catch ex As Exception
' Find the location of the first opening bracket
Dim OpeningBracket As Integer = dDate.IndexOf("(")
Dim DateLength As Integer = dDate.Length
' Remove the trailing timezone abbreviation in brackets
dDate = dDate.Remove(OpeningBracket, DateLength - OpeningBracket)
' 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
End Try
End Select
Return Converted_Date
End Function
Either I am missing something really simple here or parsing the date format into a DateTime object with timezone info, etc is a real pain.
Does anyone have any ideas on how best to accomplish this??