必須注意的是月份的起始值是從 0 開始, 也就是 0 代表 1 月, 1 代表 2 月...
這裡跟 Calendar 裡取得 Month 時一樣會少 1
方法 1
var today = new Date();
// 取得 10 天後的日期
var range = 10;
today.setDate(today.getDate() + range);
// 取得 5 天前的日期
today = new Date(); // 回到今天
range = -5;
today.setDate(today.getDate() + range);
方法 2, 自己手動算
var temp = new Date();
var range = 10;
temp = temp.valueOf();
temp = temp + (range * 24 * 60 * 60 * 1000); // 一天幾豪秒
temp = new Date(temp);
...
另外取得年月日的方法, 比較要注意的還有"年"的取得.
var year = new Date().getFullYear(); // 2010 四碼
var month = new Date().getMonth(); // 這裡會少 1, 原因上面說過, 顯示的話要記得加 1
var date = new Date().getDate();
沒有留言:
張貼留言