Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have got date as
Quote:
/Date(1352656800000)/


How to make it as Jan 11,2012.
Posted

JavaScript

Yaseer -

You can use:
JavaScript
var completedDateText = "/Date(1352656800000)/";
var completedDate = new Date(parseInt(completedDateText.replace("/Date(", "").replace(")/")));
alert(completedDate.toDateString());

Or, if you want to format your date
JavaScript
var completedDateText = "/Date(1352656800000)/";
    var completedDate = new Date(parseInt(completedDateText.replace("/Date(", "").replace(")/")));
    // from http://www.webdeveloper.com/forum/showthread.php?67608-Date()-format-as-mm-dd-yyyy
    var dd = completedDate.getDate();
    var mm = completedDate.getMonth() + 1; //January is 0! 
    var yyyy = completedDate.getFullYear(); 
    if(dd<10){dd='0'+dd} 
    if(mm<10){mm='0'+mm}
    alert('Completed date is ' + mm + '/' + dd + '/' + yyyy);
 
Share this answer
 
v2
As I see, it is a JavaScript timestamp.
Ok: 1352656800000 means Sun, 11 Nov 2012 18:00:00 GMT
So, first of all decode the JSON string to an object. Than parse it and for every matching field (since you have not given the whole structure, you have to find them), use the following function (supposing, that the field value format is /Date(1352656800000)/):

HTML
<script>
function ParseDate(input){ 
	theDate = new Date(parseInt(input.substring(6,19)));
	return theDate.toGMTString();
}

alert(ParseDate("/Date(1352656800000)/"));
</script>


If you need formatting, or you have to use the timestamp as javascript date object, just change the returned expression.
 
Share this answer
 
JavaScript
function parseJsonDate(jsonDate) {
               var offset = new Date().getTimezoneOffset() * 60000;
               var parts = /\/Date\((-?\d+)([+-]\d{2})?(\d{2})?.*/.exec(jsonDate);

               if (parts[2] == undefined)
                   parts[2] = 0;

               if (parts[3] == undefined)
                   parts[3] = 0;
               return new Date(+(parts[1]) + (parts[2]) * 3600000 + (parts[3]) * 60000);
           }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900