# 日付の足し算. N日後(N Days Later)

# Bash

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

# JavaScript

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

TODO: 整理する

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());
}

# PHP

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

TODO: 整理する

$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'));

# Python

d -= datetime.timedelta(1)

TODO: 整理する

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 週間

# 引数は days, seconds, microseconds, milliseconds, minutes, hours, weeks

# Ruby

d += 1

TODO: 整理する

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

# 月末から2ヶ月
d = Date::parse('2015-01-30')
puts d >> 2       # => 2015-03-30

# 月末から1ヶ月ずつ
d = Date::parse('2015-01-30')
puts d >> 1       # => 2015-02-28
puts d >> 1 >> 1  # => 2015-03-28 !!!

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

[ 📩 ご意見 ]