Created
November 12, 2020 02:52
-
-
Save kuboon/bd2e722d352fb7b6aeef1c2aa44f9bdb to your computer and use it in GitHub Desktop.
convert from/to google sheet date serial
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
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