Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Javascript jQuery JSON
I have got date as
Quote:
/Date(1352656800000)/

 
How to make it as Jan 11,2012.
Posted 28-Dec-12 9:20am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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);
           }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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)/):
 
<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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Yaseer -
 
You can use:
var completedDateText = "/Date(1352656800000)/";
var completedDate = new Date(parseInt(completedDateText.replace("/Date(", "").replace(")/")));
alert(completedDate.toDateString());
Or, if you want to format your date
 
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);
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 389
1 Sergey Alexandrovich Kryukov 375
2 Maciej Los 295
3 King_Fisher 160
4 Garth J Lancaster 158
0 Sergey Alexandrovich Kryukov 11,069
1 OriginalGriff 5,743
2 Maciej Los 5,649
3 CPallini 3,879
4 BillWoodruff 3,584


Advertise | Privacy | Mobile
Web03 | 2.8.141030.1 | Last Updated 29 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100