9
3

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.

[javascript]5分前の日時を取得する

Last updated at Posted at 2019-04-12

5分前の年月日&日時取得の際、取得した時の時刻が0:00の場合、日付も時間(hour)も変わってしまうためのめも。


var now = new Date();
var before5min_unix = now.getTime() - 300000; //ミリ秒なので(300秒*1000)
var before5min = new Date(before5min_unix);

var year = before5min.getFullYear();
var month = before5min.getMonth()+1;
var date = before5min.getDate();
var hours = before5min.getHours();
var minute = before5min.getMinutes();

console.log(year+'/'+month+'/'+date+'/ '+hours+':'+minute);

結果

2019/4/12/ 13:33
9
3
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
9
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?