title: Raw javascript date to VB.net datetime
body: <p>I have a javascript AJAX call passing a javascript date object to an ASP.Net MVC controller.
  Here is the AJAX call:</p>
  
  <pre><code>  var timestamp = new Date();
    $.ajax({
      url: '',
      type: 'POST',
      data: { username: username,
              timestamp: timestamp,
              hash: ''},
      dataType: 'json',
  </code></pre>
  
  <p>(I have omitted some sensitive values, but the important bits are there).</p>
  
  <p>The controller has an object for the parameters being passed which gets populated just fine.</p>
  
  <pre><code>Public Class AjaxParams
     Public Property Username As String
     Public Property Hash As String
     Public Property Timestamp As String
  End Class
  </code></pre>
  
  <p>And here is the controller declaration:</p>
  
  <pre><code>&lt;HttpPost()&gt; _
  Public Function RetrieveSalt(params As AjaxParams)
  </code></pre>
  
  <p>When making a call to the controller, all of the parameters get correctly passed and populate the AjaxParams object.</p>
  
  <p>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.</p>
  
  <p>Here is one of the failing date formats that must be converted:</p>
  
  <blockquote>
    <p>Tue Sep 17 2013 07:01:36 GMT+0600 (ALMT)</p>
  </blockquote>
  
  <p>Here is the function I created:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>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. </p>
  
  <p>Does anyone have any ideas on how best to accomplish this??</p>