即時尋找數學公式來加/減日期/月。我只需要知道年份和月份,因此可以忽略這些日子。添加和減少使用簡單的數學月
這是我想出了添加個月的僞代碼:
OldYear = 2012 // current year
OldMonth = 3 // current month
AddMonths = 0 // the months to be added
FooBar = OldMonth + AddMonths
NewYear = OldYear + FooBar/12
NewMonth = FooBar % 12
IF NewMonth = 0
NewYear = NewYear - 1
NewMonth = 12
END IF
// set AddMonths to 0 and the result will be 2012.03
// set AddMonths to 6 and the result will be 2012.09
// set AddMonths to 9 and the result will be 2012.12
// set AddMonths to 11 and the result will be 2013.02
// set AddMonths to 23 and the result will be 2014.02
// set AddMonths to 38 and the result will be 2015.05
,它真正偉大的作品,但有一個更好的方法?我真的不喜歡IF NewMonth = 0重新調整的需要。
但我的實際問題是,我不能拿出一個對應的公式來減去幾個月。我嘗試了各種各樣的東西,但一切都失敗了,它讓我瘋狂。所以任何幫助將不勝感激!
哇我永遠不會想出解決方案。非常感謝你們! :) – 2012-07-16 12:58:33