Skip to content

Instantly share code, notes, and snippets.

@kuboon
Created Nov 12, 2020
Embed
What would you like to do?
convert from/to google sheet date serial
import { zonedTimeToUtc, utcToZonedTime } from "date-fns-tz";
const SheetDate = {
origin: Date.UTC(1899, 11, 30, 0, 0, 0, 0),
dayToMs: 24 * 60 * 60 * 1000
};
function serialToDate(d: number, sheetTimeZone: string): Date {
return zonedTimeToUtc(
new Date(d * SheetDate.dayToMs + SheetDate.origin),
sheetTimeZone
);
}
function dateToSerial(date: Date, sheetTimeZone: string) {
const msec = utcToZonedTime(date, sheetTimeZone).getTime() - SheetDate.origin;
return msec / SheetDate.dayToMs;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment