/*
*判断字符串是否为日期(日期格式为:YYYY-MM-DD)
*/
function isAdDate(adDate) {
// 从1000到9999年的日期格式
var pattern = /^[1-9]/d{3}-((0[1-9]{1})|(1[0-2]{1}))-((0[1-9]{1})|([1-2]{1}/d{1})|(3[0-1]{1}))$/;
if(!pattern.test(adDate)){
return false;
}
var arrAdDate=adDate.split("-");
var adYear = parseInt(arrAdDate[0]);
var month = parseInt(arrAdDate[1]);
var day = parseInt(arrAdDate[2]);
*判断字符串是否为日期(日期格式为:YYYY-MM-DD)
*/
function isAdDate(adDate) {
// 从1000到9999年的日期格式
var pattern = /^[1-9]/d{3}-((0[1-9]{1})|(1[0-2]{1}))-((0[1-9]{1})|([1-2]{1}/d{1})|(3[0-1]{1}))$/;
if(!pattern.test(adDate)){
return false;
}
var arrAdDate=adDate.split("-");
var adYear = parseInt(arrAdDate[0]);
var month = parseInt(arrAdDate[1]);
var day = parseInt(arrAdDate[2]);
dateTmp= new Date(adYear,month-1,day);
if (dateTmp.getFullYear()!=adYear || dateTmp.getMonth()!=month-1 || dateTmp.getDate()!=day) {
return false;
}
return true;
}
if (dateTmp.getFullYear()!=adYear || dateTmp.getMonth()!=month-1 || dateTmp.getDate()!=day) {
return false;
}
return true;
}
/*
* 修改输入的日期(日期格式为:YYYY-MM-DD)
* 例如:输入1999-12-11,
* 如果要返回输入日期+3的日期,把dayoffset设置为3;
* 如果要返回输入日期-3的日期,把dayoffset设置为-3;
*/
function changeAdDate(adDate, yearoffset, monthoffset, dayoffset) {
if (!isAdDate(adDate)) {
return "";
}
var arrAdDate=adDate.split("-");
var adYear = parseInt(arrAdDate[0]);
var month = parseInt(arrAdDate[1]);
var day = parseInt(arrAdDate[2]);
* 修改输入的日期(日期格式为:YYYY-MM-DD)
* 例如:输入1999-12-11,
* 如果要返回输入日期+3的日期,把dayoffset设置为3;
* 如果要返回输入日期-3的日期,把dayoffset设置为-3;
*/
function changeAdDate(adDate, yearoffset, monthoffset, dayoffset) {
if (!isAdDate(adDate)) {
return "";
}
var arrAdDate=adDate.split("-");
var adYear = parseInt(arrAdDate[0]);
var month = parseInt(arrAdDate[1]);
var day = parseInt(arrAdDate[2]);
destDate = new Date(adYear+yearoffset, month+monthoffset-1, day+dayoffset);
adYear = destDate.getFullYear();
month = destDate.getMonth() + 1;
day = destDate.getDate();
return adYear+"-"+(month>9?month:("0"+month))+"-"+(day>9?day:("0"+day));
}
adYear = destDate.getFullYear();
month = destDate.getMonth() + 1;
day = destDate.getDate();
return adYear+"-"+(month>9?month:("0"+month))+"-"+(day>9?day:("0"+day));
}
test:
var adDate = "1999-12-21";
if (isAdDate(adDate)) {
document.write("更新前日期:"+adDate + "<br>");
var mydate = changeAdDate(adDate, 0, 0, 3);
document.write("更新后日期:"+mydate);
} else {
document.write("不是合法日期!");
}
if (isAdDate(adDate)) {
document.write("更新前日期:"+adDate + "<br>");
var mydate = changeAdDate(adDate, 0, 0, 3);
document.write("更新后日期:"+mydate);
} else {
document.write("不是合法日期!");
}