Categories
Uncategorized

Convert Date Time to Json using ASP.NET MVC and Jquery

Today, I was working with the C# DateTime datatype and Jquery, where I wanted to return DateTime as Json. I ended up getting dates in the format like /Date(1845396695590)/ or /Date(1198908717056)/.

After my little search on the internet, I found out that the value returned were actually date time value in milliseconds along with some special characters like ‘/’,’()’ and ‘Date’.

Solution

The solutions are many, I will just point out one of the easiest and efficient method which I chose to use in my application.

var milliseconds = "/Date(1245398693390)/".replace(//Date((-?d+))//, '$1');
var actualDate = new Date(parseInt(milliseconds ));

Also you might need to have a look at the Date Object of javascript, here’s a link to it.

Hope this helps 🙂