/* 往指定時間字符串上加時間間隔,得到新的時間字符串 * startDateStr:開始時間字符串,相似"2015-7-20 17:26:00" * durationNumStr:持續時間數 * durationTypeStr:持續時間類型 * 返回相似:2015-7-20 17:26:00 * * */ function getEndDate(startDateStr, durationNumStr, durationTypeStr) { var startDate = new Date(startDateStr.replace("-", "/").replace("-", "/")); var duration_num = parseInt(durationNumStr); var endDate = ""; if (durationTypeStr == "min") { endDate = new Date(startDate.setMinutes(startDate.getMinutes() + duration_num)); } if (durationTypeStr == "h") { endDate = new Date(startDate.setHours(startDate.getHours() + duration_num)); } if (durationTypeStr == "d") { endDate = new Date(startDate.setDate(startDate.getDate() + duration_num)); } if (durationTypeStr == "w") { endDate = new Date(startDate.setDate(startDate.getDate() + duration_num * 7)); } if (durationTypeStr == "m") { endDate = new Date(startDate.setMonth(startDate.getMonth() + duration_num)); } if (durationTypeStr == "y") { endDate = new Date(startDate.setFullYear(startDate.getFullYear() + duration_num)); } var year = endDate.getFullYear(); var month = endDate.getMonth() + 1 < 10 ? "0" + (endDate.getMonth() + 1) : endDate.getMonth() + 1; var date = endDate.getDate() < 10 ? "0" + endDate.getDate() : endDate .getDate(); var hour = endDate.getHours() < 10 ? "0" + endDate.getHours() : endDate .getHours(); var minute = endDate.getMinutes() < 10 ? "0" + endDate.getMinutes() : endDate.getMinutes(); var second = endDate.getSeconds() < 10 ? "0" + endDate.getSeconds() : endDate.getSeconds(); var endDateStr = year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second; return endDateStr; } /** * 計算開始時間是否小於結束時間,小於等於返回true,不然false * startDateStr:開始時間字符串 * endDateStr:結束時間字符串 * 返回:true或false * * */ function chkTime(startDateStr, endDateStr) { //計劃截止時間 var startDate = new Date(startDateStr.replace("-", "/").replace("-", "/")); //計劃詳細項的截止時間 var endDate = new Date(endDateStr.replace("-", "/").replace("-", "/")); if (startDate <= endDate) { return true; } else { return false; } }