Skip to content

Instantly share code, notes, and snippets.

@g4rcez
Created March 26, 2021 16:13
Show Gist options
  • Save g4rcez/d028b2af597894f8ba3ef7414e49b2d7 to your computer and use it in GitHub Desktop.
Save g4rcez/d028b2af597894f8ba3ef7414e49b2d7 to your computer and use it in GitHub Desktop.
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