1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

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仕様がよくない気がしている)

参考

1
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?