Skip to content

Instantly share code, notes, and snippets.

@fhdalikhan
Last active February 7, 2019 08:56
Show Gist options
  • Save fhdalikhan/8f110b7a94c1335ddc5cd5496b45981a to your computer and use it in GitHub Desktop.
Save fhdalikhan/8f110b7a94c1335ddc5cd5496b45981a to your computer and use it in GitHub Desktop.
get unix timestamp from date, date format is YYYY-MM-DD
function getUnixTimeFromDate (date) {
if ( typeof(date) === 'undefined' ) {
throw 'Date is required';
}
// using lodash _.isEmpty if available
if ( typeof window._.isEmpty === 'function' && _.isEmpty(date) ) {
throw 'Date cannot be empty.';
} else if( date === '' || date == '' ) {
throw 'Date cannot be empty.';
}
var dob = new Date(date).getTime() / 1000;
dob = parseInt(dob);
return dob;
}
// example
// console.log( getUnixTimeFromDate('2012-08-21') );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment