Created
May 17, 2017 11:56
-
-
Save kmcconnell/f4931ee58edff1f593ee1f1a7f0ea51a to your computer and use it in GitHub Desktop.
Convert an argument representing a datetime into a Date object.
This file contains 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
/** | |
* Converts an argument representing a datetime into a Date object. | |
* | |
* @param {string|number|Date} time Argument representing a datetime. | |
* | |
* @return {Date} The Date object representing the specified datetime. | |
*/ | |
function convert_to_date_object(time) | |
{ | |
switch (typeof time) { | |
case 'number': | |
break; | |
case 'string': | |
time = + new Date(time); | |
break; | |
case 'object': | |
if (time.constructor === Date) { | |
time = time.getTime(); | |
} | |
break; | |
default: | |
time = + new Date(); | |
} | |
return time; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment