LoginSignup
0
0

More than 3 years have passed since last update.

Reactで<input type="datetime-local">のvalueをセットするにはひと工夫必要

Posted at

ブラウザ実装のデフォルトのdatetime pickerを使いたいなと思ったら、想像以上に情報なくて困ったのでメモ。

実装

あまり装飾せずに、あえて最小構成でコンポーネントを作る。

import moment from 'moment';

const DatetimeComponent = () => {
  const toDatetime = (date: Date) => {
    return moment(date)
      .toISOString(true)
      .substr(0, 16);
  };
  return <input type="datetime-local" defaultValue={toDatetime(new Date())}>
}

一言

toISOStringとかで普通にセットできるようにして欲しい(Reactが悪いわけではなく、HTML仕様がよくない気がしている)

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0