0
1

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.

dateオブジェクトでのいろんな日(時間)の求め方

Last updated at Posted at 2017-03-08

JavaSCriptでのいろんな日付、時間の求め方のまとめ。

目次

ソース

共通ソース

let now = new Date();
let nowYear = now.getFullYear();
let nowMonth = now.getMonth();
let nowDate = now.getDate();
let nowhour = now.getHours();

今日の終わり

let date = new Date(nowYear, nowMonth, nowDate, 23, 59, 59);

x日前_x日後

// 例)5日前
let x = -5;
let date = new Date(nowYear, nowMonth, nowDate + x);

// 例)2日後
let x = 2;
let date = new Date(nowYear, nowMonth, nowDate + x);

x時間前_x時間後

// 例)3時間前
let x = -3;
let date = new Date(nowYear, nowMonth, nowDate, nowhour + x);

// 例)1時間後
let x = 1;
let date = new Date(nowYear, nowMonth, nowDate, nowhour + x);

月初_月末

※dateに0を指定すると、前月の最終日に設定されるため、monthに1を加算

// 月初
let date = new Date(nowYear, nowMonth, 1);

// 月末
let date = new Date(nowYear, nowMonth + 1, 0);

年始_年末

// 年始
let date = new Date(nowYear, 0, 1);

// 年末
let date = new Date(nowYear, 11, 31);
0
1
1

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?