2016-04-23 57 views

回答

1

你可以使用兩個日期

var date = new Date(2010, 3, 12); 
var date6MonthsAgo = new Date(date); 
date6MonthsAgo.setMonth(date.getMonth() - 6); 

// and to set it to the first date of the month 
date6MonthsAgo.setDate(date.getDate() - date.getDate() + 1); 
+0

我正在獲取輸出,但日期與今天的日期相同。 – Vimalraj

+0

今天的日期是23/04/2016當我執行這個代碼即時獲取23/10/2015。所以這兩個日期是一樣的,只有月份的變化是有反正改變日期月改變。 – Vimalraj

+0

另一個問題是,如果今天是3月31日,並且如果我這樣做,我會得到feb 31作爲輸出。那麼這裏的錯誤是什麼? dateoneMonthsAgo.setMonth(date.getMonth() - 1); – Vimalraj

相關問題