Skip to content

Commit

Permalink
make angular.String.toDate consider all time fractions as optional
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorMinar committed Nov 9, 2010
1 parent da17c61 commit fc9ce9e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions src/apis.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, '\\\\').
Expand Down Expand Up @@ -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;
Expand Down
17 changes: 17 additions & 0 deletions test/ApiSpecs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(){
Expand Down

0 comments on commit fc9ce9e

Please sign in to comment.