Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 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
Your Filters
Interested
Ignored
     
0 Shai Vashdi 1,398
1 OriginalGriff 508
2 Manas Bhardwaj 383
3 Damith Weerasinghe 330
4 Tadit Dash 310
0 Sergey Alexandrovich Kryukov 9,575
1 OriginalGriff 6,006
2 Peter Leow 4,500
3 Maciej Los 3,540
4 Abhinav S 3,513


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