diff options
| author | Igor Minar | 2010-11-08 10:51:28 -0800 |
|---|---|---|
| committer | Igor Minar | 2010-11-08 22:49:30 -0800 |
| commit | fc9ce9ec075aa21cdf6a79987e4aa152991f6a44 (patch) | |
| tree | d68ea23ebb8682dcccafe3890d8ff70d07a23a09 | |
| parent | da17c614441c28907047442997bd4d99cdc6eaa1 (diff) | |
| download | angular.js-fc9ce9ec075aa21cdf6a79987e4aa152991f6a44.tar.bz2 | |
make angular.String.toDate consider all time fractions as optional
| -rw-r--r-- | CHANGELOG.md | 1 | ||||
| -rw-r--r-- | src/apis.js | 7 | ||||
| -rw-r--r-- | test/ApiSpecs.js | 17 |
3 files changed, 22 insertions, 3 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index af06e98d..5b4f21d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### Api - date filter now accepts strings that angular.String.toDate can convert to Date objects +- angular.String.toDate supports ISO8061 formated strings with all time fractions being optional ### Breaking changes - we now support ISO 8601 extended format datetime strings (YYYY-MM-DDTHH:mm:ss.SSSZ) as defined diff --git a/src/apis.js b/src/apis.js index f8deb6ae..a9a9379d 100644 --- a/src/apis.js +++ b/src/apis.js @@ -177,6 +177,8 @@ var angularArray = { } }; +var R_ISO8061_STR = /^(\d{4})-(\d\d)-(\d\d)(?:T(\d\d)(?:\:(\d\d)(?:\:(\d\d)(?:\.(\d{3}))?)?)?Z)?$/ + var angularString = { 'quote':function(string) { return '"' + string.replace(/\\/g, '\\\\'). @@ -210,11 +212,10 @@ var angularString = { */ 'toDate':function(string){ var match; - if (typeof string == 'string' && - (match = string.match(/^(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)\.(\d{3})Z$/))){ + if (isString(string) && (match = string.match(R_ISO8061_STR))){ var date = new Date(0); date.setUTCFullYear(match[1], match[2] - 1, match[3]); - date.setUTCHours(match[4], match[5], match[6], match[7]); + date.setUTCHours(match[4]||0, match[5]||0, match[6]||0, match[7]||0); return date; } return string; diff --git a/test/ApiSpecs.js b/test/ApiSpecs.js index 344cf5da..81fd9155 100644 --- a/test/ApiSpecs.js +++ b/test/ApiSpecs.js @@ -190,8 +190,25 @@ describe('api', function(){ }); it('UTCtoDate', function(){ + //full ISO8061 expect(angular.String.toDate("2003-09-10T13:02:03.000Z")). toEqual(new Date("Sep 10 2003 13:02:03 GMT")); + + //no millis + expect(angular.String.toDate("2003-09-10T13:02:03Z")). + toEqual(new Date("Sep 10 2003 13:02:03 GMT")); + + //no seconds + expect(angular.String.toDate("2003-09-10T13:02Z")). + toEqual(new Date("Sep 10 2003 13:02:00 GMT")); + + //no minutes + expect(angular.String.toDate("2003-09-10T13Z")). + toEqual(new Date("Sep 10 2003 13:00:00 GMT")); + + //no time + expect(angular.String.toDate("2003-09-10")). + toEqual(new Date("Sep 10 2003 00:00:00 GMT")); }); it('StringFromUTC', function(){ |
