new Date(parseInt(JSONDateFromServerSide.substr(6))
Let's use the above code in a simple demo and then we will discuss what's the magic in the above line. Below is the sample code for the demo. this JSON date will converted to JavaScript date and different date methods are then applied to this JavaScript date.
<html>
<head id="Head1" runat="server">
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
// Add the page method call as an onclick handler for the div.
$(document).ready(function () {
debugger;
//Date fetched directly what is given from server side
var dateAsFromServerSide = "/Date(1418322600000)/";
//Now let's convert it to js format
//Example: Fri Dec 03 2010 16:37:32 GMT+0530 (India Standard Time)
var parsedDate = new Date(parseInt(dateAsFromServerSide.substr(6)));
var jsDate = new Date(parsedDate); //Date object
//Play with jsDate properties getDate(), getDay() etc
var html = 'dateAsFromServerSide:' + dateAsFromServerSide +
'<br/>' + 'parsedDate: ' + parsedDate +
'<br/>' + 'jsDate.getDay(): ' + jsDate.getDay() +
'<br/>' + 'jsDate.getDate(): ' + jsDate.getDate() +
'<br/>' + 'jsDate.getFullYear(): ' + jsDate.getFullYear() +
'<br/>' + 'jsDate.getHours(): ' + jsDate.getHours() +
'<br/>' + 'jsDate.getMilliseconds(): ' + jsDate.getMilliseconds() +
'<br/>' + 'jsDate.getMinutes(): ' + jsDate.getMinutes() +
'<br/>' + 'jsDate.getMonth(): ' + jsDate.getMonth() +
'<br/>' + 'jsDate.getSeconds(): ' + jsDate.getSeconds() +
'<br/>' + 'jsDate.getTime():' + jsDate.getTime() +
'<br/>' + 'jsDate.getTimezoneOffset(): ' + jsDate.getTimezoneOffset() +
'<br/>' + 'jsDate.getUTCDate(): ' + jsDate.getUTCDate() +
'<br/>' + 'jsDate.getUTCDay(): ' + jsDate.getUTCDay() +
'<br/>' + 'jsDate.getUTCFullYear(): ' + jsDate.getUTCFullYear() +
'<br/>' + 'jsDate.getUTCHours(): ' + jsDate.getUTCHours() +
'<br/>' + 'jsDate.getUTCMilliseconds(): ' + jsDate.getUTCMilliseconds() +
'<br/>' + 'jsDate.getUTCMinutes(): ' + jsDate.getUTCMinutes() +
'<br/>' + 'jsDate.getUTCMonth(): ' + jsDate.getUTCMonth() +
'<br/>' + 'jsDate.getUTCSeconds(): ' + jsDate.getUTCSeconds() +
'<br/>' + 'jsDate.getYear(): ' + jsDate.getYear();
//Fill html
$('#divDate').html(html);
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="divDate">
</div>
</form>
</body>
</html>
Output
dateAsFromServerSide:/Date(1418322600000)/
parsedDate: Thu Dec 11 2014 23:59:00 GMT+0529 (India Standard Time)
jsDate.getDay(): 4
jsDate.getDate(): 11
jsDate.getFullYear(): 2014
jsDate.getHours(): 23
jsDate.getMilliseconds(): 0
jsDate.getMinutes(): 59
jsDate.getMonth(): 11
jsDate.getSeconds(): 0
jsDate.getTime():1418322600000
jsDate.getTimezoneOffset(): -329
jsDate.getUTCDate(): 11
jsDate.getUTCDay(): 4
jsDate.getUTCFullYear(): 2014
jsDate.getUTCHours(): 18
jsDate.getUTCMilliseconds(): 0
jsDate.getUTCMinutes(): 30
jsDate.getUTCMonth(): 11
jsDate.getUTCSeconds(): 0
jsDate.getYear(): 114