mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-16 23:30:23 +00:00
Simplify implementation of angular.String.toDate()
This commit is contained in:
parent
4c61fc01f9
commit
42855e4363
1 changed files with 8 additions and 9 deletions
17
src/apis.js
17
src/apis.js
|
|
@ -202,16 +202,15 @@ var angularString = {
|
|||
}
|
||||
return chars.join('');
|
||||
},
|
||||
|
||||
/**
|
||||
* Tries to convert input to date and if successful returns the date, otherwise returns the input.
|
||||
* @param {string} string
|
||||
* @return {(Date|string)}
|
||||
*/
|
||||
'toDate':function(string){
|
||||
var match;
|
||||
if (typeof string == 'string' &&
|
||||
(match = string.match(/^(\d\d\d\d)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)Z$/))){
|
||||
var date = new Date(0);
|
||||
date.setUTCFullYear(match[1], match[2] - 1, match[3]);
|
||||
date.setUTCHours(match[4], match[5], match[6], 0);
|
||||
return date;
|
||||
}
|
||||
return string;
|
||||
var date = new Date(string);
|
||||
return isNaN(date.getTime()) ? string : date;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue