21
14

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.

月の日数を取得する

Posted at

dateオブジェクトを使用して簡単に求めることができます。
setDateの仕様で0を指定した場合は、前月の末日に設定されます。
この仕様を利用して下記のように求めます。

/**
 * 指定月の日数を取得
 * @param  {number} year  年
 * @param  {number} month 月
 * @return {number} 指定月の日数
 */
const getLastDay = (year, month) => {
    return new Date(year, month, 0).getDate();
};

// 28
const lastDay = getLastDay(2017, 2);
21
14
2

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
21
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?