JavaScript 指定日期按天/日/周/月/年增加(等于最终日期)

第一种的写法

/**
* 字符串转时间(yyyy-MM-dd HH:mm:ss)
* result (分钟)
*/
function stringToDate(fDate){
var fullDate = fDate.split("-");
return new Date(fullDate[0], fullDate[1]-1, fullDate[2], 0, 0, 0);
}


/**
* 格式化日期
* @param date 日期
* @param format 格式化样式,例如yyyy-MM-dd HH:mm:ss E
* @return 格式化后的
*/
function formatDate(date, format) {
var v = "";
if (typeof date == "string" || typeof date != "object") {
return;
}
var year = date.getFullYear();
var month = date.getMonth()+1;
var day = date.getDate();
var hour = date.getHours();
var minute = date.getMinutes();
var second = date.getSeconds();
var weekDay = date.getDay();
var ms = date.getMilliseconds();
var weekDayString = "";

if (weekDay == 1) {
weekDayString = "星期一";
} else if (weekDay == 2) {
weekDayString = "星期二";
} else if (weekDay == 3) {
weekDayString = "星期三";
} else if (weekDay == 4) {
weekDayString = "星期四";
} else if (weekDay == 5) {
weekDayString = "星期五";
} else if (weekDay == 6) {
weekDayString = "星期六";
} else if (weekDay == 7) {
weekDayString = "星期日";
}

v = format;
//Year
v = v.replace(/yyyy/g, year);
v = v.replace(/YYYY/g, year);
v = v.replace(/yy/g, (year+"").substring(2,4));
v = v.replace(/YY/g, (year+"").substring(2,4));

//Month
var monthStr = ("0"+month);
v = v.replace(/MM/g, monthStr.substring(monthStr.length-2));

//Day
var dayStr = ("0"+day);
v = v.replace(/dd/g, dayStr.substring(dayStr.length-2));

//hour
var hourStr = ("0"+hour);
v = v.replace(/HH/g, hourStr.substring(hourStr.length-2));
v = v.replace(/hh/g, hourStr.substring(hourStr.length-2));

//minute
var minuteStr = ("0"+minute);
v = v.replace(/mm/g, minuteStr.substring(minuteStr.length-2));

//Millisecond
v = v.replace(/sss/g, ms);
v = v.replace(/SSS/g, ms);

//second
var secondStr = ("0"+second);
v = v.replace(/ss/g, secondStr.substring(secondStr.length-2));
v = v.replace(/SS/g, secondStr.substring(secondStr.length-2));

//weekDay
v = v.replace(/E/g, weekDayString);
return v;
}

var dateValue="2014-02-28";
var cDate = stringToDate(dateValue);
cDate.setDate(cDate.getDate()+8);
currentDate = formatDate(cDate, "yyyy-MM-dd");
console.log(currentDate ); // 2014-03-01

另外一种简洁的写法

//日期加天数的方法
//dataStr日期字符串
//dayCount 要增加的天数
//return 增加n天后的日期字符串
function dateAddDays(dataStr,dayCount) {
var strdate=dataStr; //日期字符串
var isdate = new Date(strdate.replace(/-/g,"/")); //把日期字符串转换成日期格式
isdate = new Date((isdate/1000+(86400*dayCount))*1000); //日期加1天
var pdate = isdate.getFullYear()+"-"+(isdate.getMonth()+1)+"-"+(isdate.getDate());
//把日期格式转换成字符串
return pdate;
}
console.log(dateAddDays("2017-06-14",5));//2017-6-19
console.log(dateAddDays("2017-06-14",15));//2017-6-29
console.log(dateAddDays("2017-06-14",-15));//2017-5-30

如果不是字符串,是日期类型的,可以参考下面的

// srcDate:Date类型
// n:加几天,负数则为减几天
function dateAdd(srcDate,n){
return new Date(srcDate.getTime()+n*24*3600000);
}

按照周来增加和减去;

var weeks = 1;
var beginDateStr = "2017/06/14";
//创建开始时间对象
var beginDate = new Date(beginDateStr);
//设置增加的天数
beginDate.setDate(beginDate.getDate() + weeks*7);

//获取增加天数后的时间字串
//-3- js中getMonth()的返回值从0开始到11,因此要加1,才是正确的值
var endDateStr = beginDate.getFullYear() + "/" + (beginDate.getMonth()+1) + "/" + beginDate.getDate();

//输出:2007/2/5
console.log(endDateStr);//2017/6/21
console.log(beginDate.toLocaleString());//2017/6/21 上午12:00:00

使用js实现某个日期(YYYY-MM-DD),添加某个天数后,得到新的日期(YYYY-MM-DD)

使用js实现某个日期,添加某个天数后,得到新的日期

//日期加上天数得到新的日期  
//dateTemp 需要参加计算的日期,days要添加的天数,返回新的日期,日期格式:YYYY-MM-DD
function getNewDay(dateTemp, days) {
var dateTemp = dateTemp.split("-");
var nDate = new Date(dateTemp[1] + '-' + dateTemp[2] + '-' + dateTemp[0]); //转换为MM-DD-YYYY格式
var millSeconds = Math.abs(nDate) + (days * 24 * 60 * 60 * 1000);
var rDate = new Date(millSeconds);
var year = rDate.getFullYear();
var month = rDate.getMonth() + 1;
if (month < 10) month = "0" + month;
var date = rDate.getDate();
if (date < 10) date = "0" + date;
return (year + "-" + month + "-" + date);
}
var myDate = new Date();

//天
myDate.setDate(myDate.getDate() + 1);

//周
myDate.setDate(myDate.getDate() + 7);

//月
myDate.setMonth(myDate.getMonth() + 1);

//年
myDate.setYear(myDate.getYear() + 1);

JS指定日期增加天数

function AddDays(date,value)
{
return new Date(Date.parse(date) + (86400000 * value));
}
Date.prototype.format = function(format)
{
var o =
{
"M+" : this.getMonth()+1, //month
"d+" : this.getDate(), //day
"h+" : this.getHours(), //hour
"m+" : this.getMinutes(), //minute
"s+" : this.getSeconds(), //second
"q+" : Math.floor((this.getMonth()+3)/3), //quarter
"S" : this.getMilliseconds() //millisecond
}
if(/(y+)/.test(format))
format=format.replace(RegExp.$1,(this.getFullYear()+"").substr(4 - RegExp.$1.length));
for(var k in o)
if(new RegExp("("+ k +")").test(format))
format = format.replace(RegExp.$1,RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
return format;
}
var strdates="2017-10-21";
var strdays=365;
var d = strdates.replace(/-/g,'/');
var now = new Date(d);
var values = AddDays(now,strdays);
var strt= values.format("yyyy-MM-dd");
console.log(strt);//2018-10-21

日期增加天数的

//日期加天数的方法
//dataStr日期字符串
//dayCount 要增加的天数
//return 增加n天后的日期字符串
function dateAddDays(dataStr,dayCount) {
var strdate=dataStr; //日期字符串
var isdate = new Date(strdate.replace(/-/g,"/")); //把日期字符串转换成日期格式
isdate = new Date((isdate/1000+(86400*dayCount))*1000); //日期加1天
var pdate = isdate.getFullYear()+"-"+(isdate.getMonth()+1)+"-"+(isdate.getDate()); //把日期格式转换成字符串

return pdate;
}

本地字符串方法toLocaleString()得到最新时间

//获取当前时间,离开日期默认为t+1
function getLocalTime(addNum) {
var year,month,day,today,thatDay;
today = new Date().getTime();
thatDay = new Date( today + addNum*(24*60*60*1000) ).toLocaleString().substr(0,9);
year = thatDay.substr(0,4);
month = thatDay.substr(5,1);
day = thatDay.substr(7,2);
thatDay = year+"-"+month +"-"+day;
return thatDay
}
getLocalTime(2)

~~~~~

未经允许不得转载:WEB前端开发 » JavaScript 指定日期按天/日/周/月/年增加(等于最终日期)

赞 (0)