Reputation: 4655
I have an ASP.NET MVC application running on top of an SQL Server database. On one of the view, a date value is fetched from the database but somehow, Date.parse
returns NaN
when I try to parse it into a Date. When I do console.log
, the date looks like this 2016-03-29T11:54:34.94
. Obviously along the way it was converted from a DateTime
C# object into its JSON equivalent. Why would JavaScript consider it not to be a valid date yet it came straight from the database?
Upvotes: 0
Views: 36
Reputation: 1847
if you refer to below link you will notice that in javascript Date
object there are certain constructor and not all formats of date will create a valid object in javascript across different browsers.
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date
To be safe I would recommend you to pass an array like [2016, 3, 29, 12, 0, 0]
to UI and then use javascript as below
var dt = new Date(arr[0], arr[1], arr[2], arr[3], arr[4], arr[5]);
This constructor is supported across all browsers and will give you a valid date
Upvotes: 1