Skip to content

Instantly share code, notes, and snippets.

@aaronpeterson
Created July 4, 2014 16:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronpeterson/8cffb9f23e1fa1ce6198 to your computer and use it in GitHub Desktop.
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
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());
@bsed
Copy link

bsed commented Jun 2, 2015

good!

@jrenouard
Copy link

Thanks!

@Av0cadoo
Copy link

Thanks!

@ckumsta
Copy link

ckumsta commented Aug 24, 2016

Thanks!

@mandadimuralidharreddy
Copy link

Thanks

@av1m
Copy link

av1m commented Jul 2, 2019

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment