make angular.String.toDate consider all time fractions as optional

This commit is contained in:
Igor Minar 2010-11-08 10:51:28 -08:00
parent da17c61444
commit fc9ce9ec07
3 changed files with 22 additions and 3 deletions

View file

@ -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

View file

@ -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;

View file

@ -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(){