2

Here is what i'm getting from json file /Date(1435837792000+0000)/

I need to display date in the following format Oct 29, 2010 9:10:23 AM

Shasha
  • 669
  • 1
  • 8
  • 26
  • Can you add the complete json? = /Date(1435837792000+0000)/ is not valid json. – Hannes Johansson Jul 08 '15 at 05:52
  • this what im getting "PublishDate": "\/Date(1435757849000+0000)\/" – Shasha Jul 08 '15 at 05:53
  • @Hannes Johansson i know using angular = {{1288323623006 | date:'medium'}}: Oct 29, 2010 9:10:23 AM we can achieve bt value im getting is suppose to be filter out first – Shasha Jul 08 '15 at 05:54
  • I would say this is a duplicate of http://stackoverflow.com/questions/4511705/how-to-parse-json-to-receive-a-date-object-in-javascript – Hannes Johansson Jul 08 '15 at 05:56
  • Not a duplicate, but interesting anyway: [*The “right” JSON date format*](http://stackoverflow.com/questions/10286204/the-right-json-date-format). – RobG Jul 08 '15 at 05:58
  • @Hannes Johansson i just wanna know to use angularjs filter here – Shasha Jul 08 '15 at 05:58
  • @HannesJohansson–not exactly, the OP's format seems to include an offset in some unspecified value (probably hhmm but who knows since the "date" seems to be milliseconds). – RobG Jul 08 '15 at 05:59
  • That's not what the title or the question says. Update the question to actually state what you really want to know. – Hannes Johansson Jul 08 '15 at 06:00

5 Answers5

0

There is no function like format() on the Date prototype in Javascript. But there are these methods:

getDate() // Returns the date
getMonth() // Returns the month
getFullYear() // Returns the year
getHours() // Returns the hour
getMinutes() // Returns the minute
getSeconds() // Returns the second

You can form the string yourself using:

function formatDate(dt) {
    var monthNames = ["January", "February", "March", "April",
        "May", "June", "July", "August", "September",
        "October", "November", "December"];

    var h = dt.getHours(), am;
    if (h > 12) {
        am = 'pm';
        h = h - 12;
    } else {
        am = 'am';
    }

    return monthNames[dt.getMonth()].substring(0, 3) + ' ' +
        dt.getDate() + ', ' + dt.getFullYear() + ' ' +
        h + ':' + dt.getMinutes() + ':' + dt.getSeconds() + ' ' +
        am;
}

console.log(formatDate(new Date(1435837792000+0000)));

If you are finding yourselves in need of more date parsing and formatting, check out the Momentjs library. With moment, you could just do:

moment(new Date(1435837792000+0000)).format("MMM D, YYYY hh:mm:ss A")
Ananth
  • 4,227
  • 2
  • 20
  • 26
0

You can extract this value from your json.

Assign it to a scope variable in the controller. yourCtrl.js

json = { name: 'stackoverflow',
         date: 1435837792000+0000
       };
$scope.today = new Date(json.date); //This converts the date value from the json to a healthy looking date something like this `2015-07-02T11:49:52.000Z`

today.html

{{ today | date:'medium' }}

This angular filter will display your date in the desired date format. Oct 29, 2010 9:10:23 AM

EDIT:

$scope.today = Date(1435837792000+0000);
$scope.today = new Date($scope.today);

and then pipe it up as using the angular filters as follows

{{ today | date:'MMM d, y hh:mm:ss' }} or {{ today | date:'medium' }} based upon your requirement.

Satyajit Patnaik
  • 111
  • 1
  • 2
  • 12
0

First get the timestamp from JSON to a Date variable, here’s a brute match:

var json = { "PublishDate": "\/Date(1435757849000+0000)\/" };
var timestamp = parseInt(json.PublishDate.match(/\d+/)[0],10);
var date = new Date(timestamp);

Then, a mix of .toDateString() and .toLocaleTimeString(), both quirky (yet localized) and possibly unreliable non-standard, might come close:

date = date.toDateString() + ' ' + date.toLocaleTimeString('en');
alert( date === 'Wed Jul 01 2015 3:37:29 PM' );

There’s also .toGMTString() that returns Thu, 02 Jul 2015 11:49:52 GMT (RFC 1123) respectively.

dakab
  • 5,379
  • 9
  • 43
  • 67
0

Use JS to convert it to Date object & then date function on it..

<html>
<head>
<script>
function jsonDatetoJSDate(){
    var dateFromServer = "/Date(1435837792000+0000)/";
    //Javascript conversion
    var prsdDt = new Date(parseInt(dateFromServer.substr(6)));
    //getting Date Object
    var uDate = new Date(prsdDt);
    alert(uDate);
}
</script>
</head>
<body onload="javascript:jsonDatetoJSDate()">
    Use Date Function on javaScript
</body>
</html>
user2019331
  • 151
  • 1
  • 1
  • 9
0

As you mentioned in comments, if the JSON you are getting is something like

var obj = {
              "PublishDate": "\/Date(1435757849000+0000)\/"
          }

and you have no control in changing the value(since you are enclosing the Date() around slashes), then you would need to

  • escape the slashs around Date.

      obj.PublishDate.replace(/\//g, "");
    
  • and evaluate the remaining expression

      eval(obj.PublishDate.replace(/\//g, ""))
    

to get the actual Date: Wed Jul 08 2015 11:48

nalinc
  • 7,375
  • 24
  • 33