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

JavaScriptできちんと日付を取得する

Last updated at Posted at 2021-02-28

JavaScriptで正しく日付を取得する

getDateに +1するだけでは 月をまたぐ際に、32日となったりする

【正】

let date = new Date();
date.setDate(date.getDate() + 1);

【使用例】

今日から1週間を取得


let date = new Date();

for(let i = 0; i<7; i++){
date.setDate(date.getDate() + 1);
console.log(date.getDate());
}

一昨日

let date = new Date();
date.setDate(date.getDate() - 2);  

明後日

let date = new Date();
date.setDate(date.getDate() + 2);  

1週間後

let date = new Date();
date.setDate(date.getDate() + 7);  
0
1
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
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?