0
Help us understand the problem. What are the problem?

posted at

TypeScriptで日時の操作を実施する(day.js)

day.jsを利用した日時操作。

app.ts
import dayjs, { Dayjs } from 'dayjs';

const now = new Date()

console.log('now: ', now)
console.log('=====================【以降】==========================')
console.log('1分後',dayjs(now).add(1, 'm').format())
console.log('1日後',dayjs(now).add(1, 'd').format())
console.log('1時間後',dayjs(now).add(1, 'h').format())
console.log('1週間後',dayjs(now).add(1, 'w').format())
console.log('1ヶ月後',dayjs(now).add(1, 'M').format())
console.log('1年後',dayjs(now).add(1, 'y').format())

console.log('=====================【以前】==========================')
console.log('1分前',dayjs(now).add(-1, 'm').format())
console.log('1日前',dayjs(now).add(-1, 'd').format())
console.log('1時間前後',dayjs(now).add(-1, 'h').format())
console.log('1週間前',dayjs(now).add(-1, 'w').format())
console.log('1ヶ月前',dayjs(now).add(-1, 'M').format())
console.log('1年前',dayjs(now).add(-1, 'y').format())

実行結果
~/develop/node_study/dayjs_sample $ ts-node app.ts
now:  2022-06-03T05:56:25.086Z
=====================【以降】==========================
1分後 2022-06-03T14:57:25+09:00
1日後 2022-06-04T14:56:25+09:00
1時間後 2022-06-03T15:56:25+09:00
1週間後 2022-06-10T14:56:25+09:00
1ヶ月後 2022-07-03T14:56:25+09:00
1年後 2023-06-03T14:56:25+09:00
=====================【以前】==========================
1分前 2022-06-03T14:55:25+09:00
1日前 2022-06-02T14:56:25+09:00
1時間前後 2022-06-03T13:56:25+09:00
1週間前 2022-05-27T14:56:25+09:00
1ヶ月前 2022-05-03T14:56:25+09:00
1年前 2021-06-03T14:56:25+09:00

Register as a new user and use Qiita more conveniently

  1. You can follow users and tags
  2. you can stock useful information
  3. You can make editorial suggestions for articles
What you can do with signing up
0
Help us understand the problem. What are the problem?