From ac4318a2fa5c6d306dbc19466246292a81767fca Mon Sep 17 00:00:00 2001 From: Igor Minar Date: Mon, 26 Mar 2012 23:38:20 -0700 Subject: refactor(fromJson/date filter): move date string logic to date filter Breaks angular.fromJson which doesn't deserialize date strings into date objects. This was done to make fromJson compatible with JSON.parse. If you do require the old behavior - if at all neeeded then because of json deserialization of XHR responses - then please create a custom $http transform: $httpProvider.defaults.transformResponse.push(function(data) { // recursively parse dates from data object here // see code removed in this diff for hints }); Closes #202 --- src/ng/filter/filters.js | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'src/ng') diff --git a/src/ng/filter/filters.js b/src/ng/filter/filters.js index 078c54fc..c792cace 100644 --- a/src/ng/filter/filters.js +++ b/src/ng/filter/filters.js @@ -288,7 +288,8 @@ var DATE_FORMATS_SPLIT = /((?:[^yMdHhmsaZE']+)|(?:'(?:[^']|'')*')|(?:E+|y+|M+|d+ * (e.g. `"h o''clock"`). * * @param {(Date|number|string)} date Date to format either as Date object, milliseconds (string or - * number) or ISO 8601 extended datetime string (yyyy-MM-ddTHH:mm:ss.SSSZ). + * number) or various ISO 8601 datetime string formats (e.g. yyyy-MM-ddTHH:mm:ss.SSSZ and it's + * shorter versions like yyyy-MM-ddTHH:mmZ, yyyy-MM-dd or yyyyMMddTHHmmssZ). * @param {string=} format Formatting rules (see Description). If not specified, * `mediumDate` is used. * @returns {string} Formatted string or the input if input is not recognized as date/millis. @@ -317,6 +318,27 @@ var DATE_FORMATS_SPLIT = /((?:[^yMdHhmsaZE']+)|(?:'(?:[^']|'')*')|(?:E+|y+|M+|d+ */ dateFilter.$inject = ['$locale']; function dateFilter($locale) { + + + var R_ISO8601_STR = /^(\d{4})-?(\d\d)-?(\d\d)(?:T(\d\d)(?::?(\d\d)(?::?(\d\d)(?:\.(\d{3}))?)?)?(Z|([+-])(\d\d):?(\d\d)))?$/; + function jsonStringToDate(string){ + var match; + if (match = string.match(R_ISO8601_STR)) { + var date = new Date(0), + tzHour = 0, + tzMin = 0; + if (match[9]) { + tzHour = int(match[9] + match[10]); + tzMin = int(match[9] + match[11]); + } + date.setUTCFullYear(int(match[1]), int(match[2]) - 1, int(match[3])); + date.setUTCHours(int(match[4]||0) - tzHour, int(match[5]||0) - tzMin, int(match[6]||0), int(match[7]||0)); + return date; + } + return string; + } + + return function(date, format) { var text = '', parts = [], -- cgit v1.2.3