Created
July 4, 2014 16:07
-
-
Save aaronpeterson/8cffb9f23e1fa1ce6198 to your computer and use it in GitHub Desktop.
Part of my AngularJS datetime service to smooth out working with mysql datetime format in javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var when = { | |
/** | |
* Convert Dateish to js Date | |
* @param mixed string|Date dateish date-like string for Date constructor | |
* @return Date js Date or null | |
*/ | |
its: function(dateish) { | |
if (!dateish) { | |
return null; | |
} | |
if (Object.prototype.toString.call(dateish) === '[object Date]') { | |
return dateish; | |
} | |
var mysqlRegEx = /^(\d\d\d\d)-(\d?\d)-(\d?\d) (\d\d):(\d\d):(\d\d)$/g; | |
var mysqlMatch = mysqlRegEx.exec(dateish); | |
var d; | |
if (mysqlMatch !== null) { | |
d = new Date(Date.UTC( | |
mysqlMatch[1], | |
mysqlMatch[2] - 1, | |
mysqlMatch[3], | |
mysqlMatch[4], | |
mysqlMatch[5], | |
mysqlMatch[6])); | |
} else { | |
d = new Date(dateish); | |
} | |
return d; | |
} | |
} | |
console.log(when.its('2014-06-30 07:15:00')); | |
console.log(when.its('2014-06-3 07:15:00')); | |
console.log(when.its('2014-06-30')); | |
console.log(when.its('2014-06-30T07:15:00.000Z')); | |
console.log(when.its(new Date('2014-06-30T07:15:00.000Z'))); | |
console.log(when.its()); |
Thanks!
Thanks!
Thanks!
Thanks
Thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
good!