Unbreakable Unbreakable - 1 year ago 62
Javascript Question

How to correctly capture parse Date in REST Service

I have one calendar in my front end and as soon as I select one date the request should go to the REST Service at server. Currently I am able to get the date string at Server but how can I get the actual date object out of that "String" which I received as "DATE"

Below is what I have tried till now.
Date Selected at front end: 10th August 2016

Equivalent Date received as String at backend: "1470873599000"

QUESTION 1: How can I create a Date object and store this String received. Eventually I need to pass date as #8/10/2016# to my actual code from the string received. Please guide me.

QUESTION 2: OR Do I need to change the way I have sent the Date from my Javascript code.



localStorage.setItem('date', start._d.getTime()); // 1470873599000
localStorage.getItem('date') // It is sent via AJAX CALL

The REST Service which I have written in VB.net to capture the date:

'getter setter for the date
Private dateId As String
Public Property dateProp() As String
Return dateId
End Get
Set(ByVal value As String)
dateId = value
End Set
End Property

Answer Source

I'd recommend changing the javascript to

localStorage.setItem('date', start._d.toISOString());

which will give a readable date/time/timezone


and to parse it on the server

Dim myInput = "2016-08-16T15:49:49.574Z"
Dim theDate = DateTime.Parse(myInput)