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 10:20am
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
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 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  

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

  Print Answers RSS
0 OriginalGriff 584
1 Maciej Los 275
2 DamithSL 233
3 Sergey Alexandrovich Kryukov 209
4 BillWoodruff 200
0 OriginalGriff 6,803
1 Sergey Alexandrovich Kryukov 6,377
2 DamithSL 5,421
3 Manas Bhardwaj 4,841
4 Maciej Los 4,330


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.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