Skip to content

Instantly share code, notes, and snippets.

@kwokhou
Last active March 1, 2016 08:33
Show Gist options
  • Save kwokhou/5873356 to your computer and use it in GitHub Desktop.
Save kwokhou/5873356 to your computer and use it in GitHub Desktop.
AngularJS filter to format ASP.NET JSON Date
// Format a /Date(XXXXXXXXXXXXXXXX)/ into a JSON date object.
angular.module('jsonDate', []).filter('jsonDate', function () {
return function (input, format) {
if (angular.isUndefined(input))
return;
// first 6 character is the date
var date = new Date(parseInt(input.substr(6)));
// default date format
if (angular.isUndefined(format))
format = "MM/DD/YYYY";
format = format.replace("DD", (date.getDate() < 10 ? '0' : '') + date.getDate()); // Pad with '0' if needed
format = format.replace("MM", (date.getMonth() < 9 ? '0' : '') + (date.getMonth() + 1)); // Months are zero-based
format = format.replace("YYYY", date.getFullYear());
return format;
};
});
@nmvuong92
Copy link

Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment