LoginSignup
2
2

More than 5 years have passed since last update.

JSで当日日付〜数日前を計算して Y/m/d H:i:s で出力したい

Last updated at Posted at 2015-10-08

何かもっとスマートな方法は無いのかな?と思いつつも下記を書いてrenderの前にぶっこみました。

_calcDatetime: function(past_day) {
    var today = new Date();
    today.setDate(today.getDate() - past_day)
    var year = today.getFullYear();
    var month = ((today.getMonth() + 1) < 10) ? '0' + (today.getMonth() + 1) : (today.getMonth() + 1);
    var day = (today.getDate() < 10) ? '0' + today.getDate() : today.getDate();
    var hour = (today.getHours() < 10)  ? '0' + today.getHours() : today.getHours();
    var min = (today.getMinutes() < 10) ? '0' + today.getMinutes() : today.getMinutes();
    var sec = (today.getSeconds() < 10) ? '0' + today.getSeconds() : today.getSeconds();
    return year + '/' + month + '/' + day + ' ' + hour + ':' + min + ':' sec;
},

ただのJavaScriptです。
他に良い書き方があったら教えて欲しいです。

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