/* * 根据传入日期,要处理的天数,进行加/减计算。 * 日期格式:XXXX年XX月XX日 */ function addByTransDate(dateParameter, num) { var translateDate = "", dateString = "", monthString = "", dayString = ""; translateDate = dateParameter.replace("-", "/").replace("-", "/");; var newDate = new Date(translateDate); newDate = newDate.valueOf(); newDate = newDate + num * 24 * 60 * 60 * 1000; newDate = new Date(newDate); //如果月份长度少于2,则前加 0 补位 if ((newDate.getMonth() + 1).toString().length == 1) { monthString = 0 + "" + (newDate.getMonth() + 1).toString(); alert(translateDate); } else { monthString = (newDate.getMonth() + 1).toString(); alert(translateDate); } //如果天数长度少于2,则前加 0 补位 if (newDate.getDate().toString().length == 1) { dayString = 0 + "" + newDate.getDate().toString(); } else { dayString = newDate.getDate().toString(); } dateString = newDate.getFullYear() + "-" + monthString + "-" + dayString; return dateString; } function reduceByTransDate(dateParameter, num) { var translateDate = "", dateString = "", monthString = "", dayString = ""; translateDate = dateParameter.replace("-", "/").replace("-", "/");; var newDate = new Date(translateDate); newDate = newDate.valueOf(); newDate = newDate - num * 24 * 60 * 60 * 1000; newDate = new Date(newDate); //如果月份长度少于2,则前加 0 补位 if ((newDate.getMonth() + 1).toString().length == 1) { monthString = 0 + "" + (newDate.getMonth() + 1).toString(); alert(translateDate); } else { monthString = (newDate.getMonth() + 1).toString(); alert(translateDate); } //如果天数长度少于2,则前加 0 补位 if (newDate.getDate().toString().length == 1) { dayString = 0 + "" + newDate.getDate().toString(); } else { dayString = newDate.getDate().toString(); } dateString = newDate.getFullYear() + "-" + monthString + "-" + dayString; return dateString; }