AngularJS how to format date in object, return JSON from Controller

2,453 views
Skip to first unread message

William Snell

unread,
Dec 20, 2012, 10:00:02 AM12/20/12
to ang...@googlegroups.com
I'm working with the jQuery Event Calendar, which processes dates in Unix timestamp format. I need to leave the JSON file alone in that regard. I also want to be able to grab the events listed in the JSON file and list them in a separate container from the calendar. I'm using AngularJS to render the repeated values. This works just fine, but where I need a date and time in their respective places, I have a long string of numbers. I can easily just create a new Date object in JavaScript and that handles the conversion for me, but I have a couple problems. First, where do I perform this? I placed a couple format functions to get the date and the time in the correct format. I need the date and time separately, so I can add a property to my collection of objects and run the timestamp through both, but this seems clunky to me. I'm wondering if there's a good way to perform the formatting in the View via AngularJS. My second question has to do with the JSON source. The calendar expects to get a JSON object fed to it, and I'd rather not have a JSON file for it and a Controller file with the same data for AngularJS. I'm very new to AngularJS, and getting some things running has been a pain. I'm hoping there's an easy way to retrieve the Controller data and convert it to a JSON object my calendar can consume.


Michael Bielski

unread,
Dec 20, 2012, 10:30:53 AM12/20/12
to ang...@googlegroups.com
I did my date manipulation in a filter. That way, it only gets applied where I want it to. See below:

"use strict";

/* App Module */
angular.module("dateCleaner", []).
filter('cleanDate', function () {
return function (inputDate, format, useBlank) {
var output;
var blankValue = "Never"
var dString = new Date(inputDate);
switch (format) {
case "locale":
console.debug("using locale date/time");
output = dString.toLocaleDateString() + " " + dString.toLocaleTimeString();
break;
default:
output = dString.toDateString() + " " + dString.getHours() + ":" + dString.getMinutes() + ":" + dString.getSeconds();
break;
}
if (useBlank) {
blankValue = "";
}
console.debug("DateFilter output = >" + output + "<");
return output.search("Invalid Date") >= 0 ? blankValue : output;
}
});
Reply all
Reply to author
Forward
0 new messages