13
13

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 5 years have passed since last update.

Moment.jsで週・月・年の操作

Last updated at Posted at 2019-08-20

日付操作って使うよね。せっかくまとめたし残しておく。

ちなみに、以下の提供サイトでDevTools開けばお気軽に検証可。
https://momentjs.com/

週単位 (月曜はじまり

//先週初
moment().add('isoWeek', -1).startOf('isoWeek').format("YYYY/MM/DD HH:mm:ss")
//先週末
moment().add('isoWeek', -1).endOf('isoWeek').format("YYYY/MM/DD HH:mm:ss")

//今週末
moment().startOf('isoWeek').format("YYYY/MM/DD HH:mm:ss")
//今週末
moment().endOf('isoWeek').format("YYYY/MM/DD HH:mm:ss")

//来週末
moment().add('isoWeek', 1).startOf('isoWeek').format("YYYY/MM/DD HH:mm:ss")
//今週末
moment().add('isoWeek', 1).endOf('isoWeek').format("YYYY/MM/DD HH:mm:ss")

月単位

//前月初
moment().add('month', -1).startOf('month').format("YYYY/MM/DD HH:mm:ss")
// 前月末
moment().add('month', -1).endOf('month').format("YYYY/MM/DD HH:mm:ss")

//今月初
moment().startOf('month').format("YYYY/MM/DD HH:mm:ss")
//今月末
moment().endOf('month').format("YYYY/MM/DD HH:mm:ss")

//来月末
moment().add('month', 1).startOf('month').format("YYYY/MM/DD HH:mm:ss")
//来月末
moment().add('month', 1).endOf('month').format("YYYY/MM/DD HH:mm:ss")

年単位

//前年初
moment().add('year', -1).startOf('year').format("YYYY/MM/DD HH:mm:ss")
//前年末
moment().add('year', -1).endOf('year').format("YYYY/MM/DD HH:mm:ss")

//今年初
moment().startOf('year').format("YYYY/MM/DD HH:mm:ss")
//今年末
moment().endOf('year').format("YYYY/MM/DD HH:mm:ss")

//来年初
moment().add('year', 1).startOf('year').format("YYYY/MM/DD HH:mm:ss")
//来年末
moment().add('year', 1).endOf('year').format("YYYY/MM/DD HH:mm:ss")

Moment.js 最高

13
13
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
13
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?