Format Date time in AngularJS
Your code should work as you have it see this fiddle.
You'll have to make sure your v.Dt
is a proper Date object for it to work though.
{{dt | date:'yyyy-MM-dd HH:mm:ss Z'}}
or if dateFormat is defined in scope as dateFormat = 'yyyy-MM-dd HH:mm:ss Z':
{{dt | date:dateFormat }}
v.Dt is likely not a Date() object.
See http://jsfiddle.net/southerd/xG2t8/
but in your controller:
scope.v.Dt = Date.parse(scope.v.Dt);
I know this is an old item, but thought I'd throw in another option to consider.
As the original string doesn't include the "T" demarker, the default implementation in Angular doesn't recognize it as a date. You can force it using new Date, but that is a bit of a pain on an array. Since you can pipe filters together, you might be able to use a filter to convert your input to a date and then apply the date: filter on the converted date. Create a new custom filter as follows:
app
.filter("asDate", function () {
return function (input) {
return new Date(input);
}
});
Then in your markup, you can pipe the filters together:
{{item.myDateTimeString | asDate | date:'shortDate'}}
you can get the 'date' filter like this:
var today = $filter('date')(new Date(),'yyyy-MM-dd HH:mm:ss Z');
This will give you today's date in format you want.