原生js获取日期
var formatDate10 = function(date){ //console.log(date); var year = date.getFullYear(); var month = date.getMonth(); month = parseInt(month)+1; if(month<10){ month = "0"+month; } var day = date.getDate(); if(day<10){ day = "0"+day; } return year+"-"+month+"-"+day; } var GetEndTime = function () { //保障期间--单位天 this.addmulDate=function(dateParameter, num) { var translateDate = "", dateString = "", monthString = "", dayString = ""; dateParameter = formatDate10(new Date(dateParameter)); translateDate = dateParameter.replace("-", "/").replace("-", "/"); var newDate = new Date(translateDate); newDate = newDate.valueOf(); newDate = newDate + (num>0?(num-1):num) * 24 * 60 * 60 * 1000; //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(); } else { monthString = (newDate.getMonth() + 1).toString(); } //如果天数长度少于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; } //保障期间--单位月 this.addmulMonth=function (dtstr, n) { var s = dtstr.split("-"); var yy = parseInt(s[0],10); var mm = parseInt(s[1],10) - 1; var dd = parseInt(s[2],10); var dt = new Date(yy, mm, dd); var dateString = "", monthString = "", dayString = ""; dt.setMonth(dt.getMonth() + n); if ((dt.getYear() * 12 + dt.getMonth()) > (yy * 12 + mm + n)) { dt = new Date(dt.getYear(), dt.getMonth(), 0); } //如果月份长度少于2,则前加 0 补位 if ((dt.getMonth() + 1).toString().length == 1) { monthString = 0 + "" + (dt.getMonth() + 1).toString(); } else { monthString = (dt.getMonth() + 1).toString(); } //如果天数长度少于2,则前加 0 补位 if (dt.getDate().toString().length == 1) { dayString = 0 + "" + dt.getDate().toString(); } else { dayString = dt.getDate().toString(); } //return dt.getFullYear() + "-" + monthString + "-" + dayString; var yearDay = dt.getFullYear() + "-" + monthString + "-" + dayString; return this.addmulDate(yearDay,0); } //保障期间--单位年 this.addmulYear=function (dtstr, n) { var s = dtstr.split("-"); var yy = parseInt(s[0],10); var mm = parseInt(s[1],10) - 1; var dd = parseInt(s[2],10); var dt = new Date(yy, mm, dd); var dateString = "", monthString = "", dayString = ""; dt.setFullYear(dt.getFullYear() + n); if ((dt.getYear() * 12 + dt.getMonth()) > (yy * 12 + mm + n)) { dt = new Date(dt.getYear(), dt.getMonth(), 0); } //如果月份长度少于2,则前加 0 补位 if ((dt.getMonth() + 1).toString().length == 1) { monthString = 0 + "" + (dt.getMonth() + 1).toString(); } else { monthString = (dt.getMonth() + 1).toString(); } //如果天数长度少于2,则前加 0 补位 if (dt.getDate().toString().length == 1) { dayString = 0 + "" + dt.getDate().toString(); } else { dayString = dt.getDate().toString(); } //return dt.getFullYear() + "-" + monthString + "-" + dayString; var yearDay = dt.getFullYear() + "-" + monthString + "-" + dayString; return this.addmulDate(yearDay,0); } } var getEndTime = new GetEndTime()