日時

1import { format, formatDistanceToNow, parse, subMinutes } from 'date-fns';
2import { ja } from 'date-fns/locale';
3import React, { useEffect, useState } from 'react';
4
5const DateFormat = () => {
6  const [now, setNow] = useState<Date>(new Date());
7  const past = parse('2022-01-01', 'yyyy-MM-dd', new Date());
8
9  useEffect(() => {
10    const interval = setInterval(() => {
11      setNow(new Date());
12    }, 1000);
13
14    return () => {
15      clearInterval(interval);
16    };
17  }, []);
18
19  return (
20    <div>
21      <p>
22        {format(now, 'yyyy/MM/dd(E) HH:mm:ss', {
23          locale: ja,
24        })}
25      </p>
26      <p>
27        {format(past, 'yyyy/MM/dd')}28        {formatDistanceToNow(past, {
29          locale: ja,
30          addSuffix: true,
31        })}
32      </p>
33    </div>
34  );
35};
36
37export default DateFormat;

使用ライブラリ

参考サイト