亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用JavaScript添加/減去日期?

如何使用JavaScript添加/減去日期?

三國紛爭 2019-10-18 10:42:48
我想讓用戶使用JavaScript輕松地添加和減去日期,以便按日期瀏覽其條目。日期的格式為:“ mm / dd / yyyy”。我希望他們能夠單擊“下一步”按鈕,并且如果日期是:“ 06/01/2012”,則單擊下一步時,它應該變成:“ 06/02/2012”。如果他們單擊“上一個”按鈕,則它應變為“ 05/31/2012”。它需要跟蹤of年,每月的天數等。有任何想法嗎?使用AJAX從服務器獲取日期的PS不是一種選擇,這有點滯后,而不是客戶端想要的用戶體驗。
查看完整描述

3 回答

?
烙印99

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)


查看完整回答
反對 回復 2019-10-18
?
qq_花開花謝_0

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


查看完整回答
反對 回復 2019-10-18
  • 3 回答
  • 0 關注
  • 556 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號