Created
March 26, 2021 16:13
-
-
Save g4rcez/d028b2af597894f8ba3ef7414e49b2d7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 { formatISO,parse } from "date-fns"; | |
const getUtcDate = (date: Date) => { | |
const r = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}).(\d{3})/; | |
const [, ...s] = r.exec(date.toISOString())!; | |
return (Date.UTC as any)(...s); | |
}; | |
const parseFromFormat = (strDate: string, format: string) => { | |
const p = parse("2020.10.06 09:24:06", "yyyy.MM.dd HH:mm:ss", new Date()); | |
const utc = getUtcDate(p); | |
const date = new Date(utc) | |
return { date, epoch: utc, iso8601: formatISO(date) } | |
} | |
console.log( parseFromFormat("2020.10.06 09:24:06", "yyyy.MM.dd HH:mm:ss") ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment