Skip to content

Instantly share code, notes, and snippets.

@westc
Created August 12, 2019 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save westc/4f250fc989d65c5f34a64990272a0e65 to your computer and use it in GitHub Desktop.
Save westc/4f250fc989d65c5f34a64990272a0e65 to your computer and use it in GitHub Desktop.
Function used to convert a numeric value (from Excel or Google Sheets) into a JavaScript Date object.
/**
* Converts a numeric value (from Excel or Google Sheets) into a JavaScript Date object.
* @param {number} numDate - The numeric value that will be converted into a date.
* @param {?number=} opt_tzMinOffset - Optional. Defaults to the minute offset of the
* local timezone. If given this should indicate how many minutes to offset the date
* by to correctly represent the desired timezone.
* @returns {Date} - The date representation of the numeric value passed in.
*/
function fromExcelDate(num, opt_tzMinOffset) {
return new Date(
(num - 25569) * 864e5
+ (opt_tzMinOffset !== undefined ? -opt_tzMinOffset || 0 : (new Date).getTimezoneOffset()) * 6e4
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment