Skip to content

Instantly share code, notes, and snippets.

@maxmckenzie
Last active September 20, 2021 09:37
Show Gist options
  • Select an option

  • Save maxmckenzie/d345b47f261ef5716cc7618c2c82fd29 to your computer and use it in GitHub Desktop.

Select an option

Save maxmckenzie/d345b47f261ef5716cc7618c2c82fd29 to your computer and use it in GitHub Desktop.

Revisions

  1. maxmckenzie revised this gist Sep 20, 2021. 1 changed file with 8 additions and 1 deletion.
    9 changes: 8 additions & 1 deletion date.jsx
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,16 @@
    const [myDate, setMyDate] = useState()

    const pad2 = (num: number): string => num < 10 ? '0' + num : num.toString();

    const utcAsLocalString = (date: Date | undefined): string | undefined => {
    if (date == null) return undefined;
    return (date.getFullYear() + '-' + pad2(date.getMonth()) + '-' + date.getDate() + 'T' + pad2(date.getHours()) + ':' + pad2(date.getMinutes()) + ':' + pad2(date.getSeconds()))
    }

    <input
    type="datetime-local"
    class="form-control"
    onChange={(e) => setMyDate(new Date((e.target as any).value))}
    value={myDate ? myDate.toISOString().split('.')[0] : null}
    value={myDate ? utcAsLocalString(myDate) : null}
    onFocus={() => {}}
    />
  2. maxmckenzie created this gist Sep 20, 2021.
    9 changes: 9 additions & 0 deletions date.jsx
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    const [myDate, setMyDate] = useState()

    <input
    type="datetime-local"
    class="form-control"
    onChange={(e) => setMyDate(new Date((e.target as any).value))}
    value={myDate ? myDate.toISOString().split('.')[0] : null}
    onFocus={() => {}}
    />