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 );
添加回答
举报