3 回答

TA貢獻1829條經驗 獲得超13個贊
碼:
var date = new Date('2011', '01', '02');
alert('the original date is ' + date);
var newdate = new Date(date);
newdate.setDate(newdate.getDate() - 7); // minus the date
var nd = new Date(newdate);
alert('the new date is ' + nd);
使用Datepicker:
$("#in").datepicker({
minDate: 0,
onSelect: function(dateText, inst) {
var actualDate = new Date(dateText);
var newDate = new Date(actualDate.getFullYear(), actualDate.getMonth(), actualDate.getDate()+1);
$('#out').datepicker('option', 'minDate', newDate );
}
});
$("#out").datepicker();
JSFiddle演示
可能會派上用場的其他東西:
getDate() Returns the day of the month (from 1-31)
getDay() Returns the day of the week (from 0-6)
getFullYear() Returns the year (four digits)
getHours() Returns the hour (from 0-23)
getMilliseconds() Returns the milliseconds (from 0-999)
getMinutes() Returns the minutes (from 0-59)
getMonth() Returns the month (from 0-11)
getSeconds() Returns the seconds (from 0-59)

TA貢獻1835條經驗 獲得超7個贊
您需要在javascript Date對象中使用getTime()和setTime()添加或減去時間。達到第1、30、31等個月的限制時,使用setDate()和getDate()將導致錯誤。
使用setTime允許您設置偏移量(以毫秒為單位),并讓Date對象計算其余部分:
var now=new Date();
var yesterdayMs = now.getTime() - 1000*60*60*24*1; // Offset by one day;
now.setTime( yesterdayMs );
添加回答
舉報