Reputation: 1117
I am getting date format like this /Date(1495111091673)/
.
I have created one custom filter to change date format.
app.filter('jsonDate', function () {
return function (date) {
return new Date(date.match(/\d+/)[0] * 1);
}
})
This filter returns date like this.
Thu May 18 2017 18:08:11 GMT+0530 (India Standard Time)
But I want it as standard format like dd/MM/yyyy
so I have edited my filter code like this:
app.filter('jsonDate', function () {
return function (date) {
return new Date(date.match(/\d+/)[0] * 1, 'dd MMMM @ HH:mm:ss');
}
})
Is it correct?
Upvotes: 2
Views: 74
Reputation: 164897
This filter returns date like this
Thu May 18 2017 18:08:11 GMT+0530 (India Standard Time)
No it doesn't, that's just how your console (or whatever) is choosing to display the Date
instance (via Date.prototype.toString()
).
I'd just use AngularJS's date
filter ~ https://docs.angularjs.org/api/ng/filter/date.
For example (where dateFormat
is your "/Date(1495111091673)/"
formatted string)
{{dateFormat | jsonDate | date : 'shortDate'}}
Or in JS
let parsed = $filter('jsonDate')(dateFormat)
let dateString = $filter('date')(parsed, 'shortDate')
or via DI
.controller('controllerName', ['dateFilter', 'jsonDateFilter',
function(dateFilter, jsonDateFilter) {
let dateString = dateFilter(jsonDateFilter(dateFormat), 'shortDate')
}])
Upvotes: 1