Skip to main content

N日後

Python

py
d -= datetime.timedelta(1)

TODO: 整理する

py
d += datetime.timedelta(1)          # [o] 1 day later              (date class)
d += datetime.timedelta(weeks=1) # [o] 1 week later (date class)
d += datetime.timedelta(months=1) # [x] (error) 1 months later
t += datetime.timedelta(seconds=1) # [o] 1 seconds later (datetime class)

dt - datetime.timedelta(30) # 30 日前
dt - datetime.timedelta(weeks=3) # 3 週間

PHP

php
$dt->modify('1 day');    // 1 day later [destructive]

TODO: 整理する

php
$dt->modify('1 day');    // 1 day later [destructive]
$dt->modify('1 month'); // 1 month later [destructive]
$dt->modify('1 second'); // 1 second later [destructive]

// 25日から1ヶ月
$dt = new DateTime('2015-01-25');
$dt # 2015-01-25
$dt->modify('1 month') # 2015-03-02 !!!
$dt->modify('1 month') # 2015-04-02 ...

// 月末から2ヶ月
$dt = new DateTime('2015-01-30');
$dt # 2015-01-30
$dt->modify('2 month') # 2015-03-30

// 月末から1ヶ月ずつ
$dt = new DateTime('2015-01-30');
$dt # 2015-01-30
$dt->modify('1 month') # 2015-03-02 !!!
$dt->modify('1 month') # 2015-04-02 ...

$dt = new DateTime('2015-01-30 14:32:25.333333');
$this->assertEquals('2015-03-30 14:32:25' , $dt->modify('2 month')->format('Y-m-d H:i:s'));

JavaScript

js
d.setDate(d.getDate() - 1)

TODO: 整理する

js
d.setDate(d.getDate() + 1)
d.setMonth(d.getMonth() + 1)
d.setSeconds(d.getSeconds() + 1)

// 25日から1ヶ月
d = new Date('2015/01/25');
d.setMonth(d.getMonth() + 1); // 2015-02-25
d.setMonth(d.getMonth() + 1); // 2015-03-25

// 月末から2ヶ月
d = new Date('2015/01/30');
assert.equal(formatDate(d), '2015/03/30');

// 月末から1ヶ月ずつ
d = new Date('2015/01/30');
d.setMonth(d.getMonth() + 1); // 2015-03-02 !!!
d.setMonth(d.getMonth() + 1); // 2015-04-02 ...

// ちなみにこのようにしても結果は同じ
function addMonth(d, m) {
return new Date(d.getFullYear(), d.getMonth() + m, d.getDate(), d.getHours(), d.getMinutes(), d.getSeconds(), d.getMilliseconds());
}

Bash

bash
date --date '2 days' # 2 days later

Ruby

rb
d += 1

TODO: 整理する

rb
d += 1   # 1 day later     (Date class)
d >>= 1 # 1 month later (Date class)
t += 1 # 1 second later (Time class)

d = Date::parse('2015-01-30')
puts d >> 2 # => 2015-03-30

d = Date::parse('2015-01-30')
puts d >> 1 # => 2015-02-28
puts d >> 1 >> 1 # => 2015-03-28 !!!

d = Date::parse('2015-01-30')
puts d >>= 1 # => 2015-02-28
puts d >>= 1 # => 2015-03-28 !!!