js的一些时间处理方法:日期差、日期转换字符串格式、获取当前年月日、日期时间的加减:加年,加月,加日,加时,加分,加秒
一、js求两个日期之间的差:传入的日期格式为:YYYY-MM-dd
【此原网址中还有其他js处理方法:http://www.cnblogs.com/Jacklovely/p/5888281.html】
//+--------------------------------------------------- //| 求两个时间的天数差 日期格式为 YYYY-MM-dd //+--------------------------------------------------- function daysBetween(DateOne,DateTwo) { var OneMonth = DateOne.substring(5,DateOne.lastIndexOf ('-')); var OneDay = DateOne.substring(DateOne.length,DateOne.lastIndexOf ('-')+1); var OneYear = DateOne.substring(0,DateOne.indexOf ('-')); var TwoMonth = DateTwo.substring(5,DateTwo.lastIndexOf ('-')); var TwoDay = DateTwo.substring(DateTwo.length,DateTwo.lastIndexOf ('-')+1); var TwoYear = DateTwo.substring(0,DateTwo.indexOf ('-')); var cha=((Date.parse(OneMonth+'/'+OneDay+'/'+OneYear)- Date.parse(TwoMonth+'/'+TwoDay+'/'+TwoYear))/86400000); return Math.abs(cha); }
二、js日期格式转换:转成YYYY-MM-dd格式
//时间格式化
Date.prototype.Format = function (fmt) { //author: meizz
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
三、获取当前年月日:2017-10-30
//获取当前年月日 var d = new Date(); var dateStr = d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate();
//获取当前月份:2022年7月16日更新,结果返回:2022-07
getCurrentMonth() {
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth() + 1;
month = month > 9 ? month : "0" + month;
var nowMonth = year + "-" + month;
return nowMonth;
},
四、日期时间的加减:加年,加月,加日,加时,加分,加秒
function DateAdd(interval,number,date) { /* * 功能:实现VBScript的DateAdd功能. * 参数:interval,字符串表达式,表示要添加的时间间隔. * 参数:number,数值表达式,表示要添加的时间间隔的个数. * 参数:date,时间对象. * 返回:新的时间对象. * var now = new Date(); * var newDate = DateAdd( "d ",5,now); *--------------- DateAdd(interval,number,date) ----------------- */ switch(interval) { case "y " : { date.setFullYear(date.getFullYear()+number); return date; break; } case "q " : { date.setMonth(date.getMonth()+number*3); return date; break; } case "m " : { date.setMonth(date.getMonth()+number); return date; break; } case "w " : { date.setDate(date.getDate()+number*7); return date; break; } case "d " : { date.setDate(date.getDate()+number); return date; break; } case "h " : { date.setHours(date.getHours()+number); return date; break; } case "m " : { date.setMinutes(date.getMinutes()+number); return date; break; } case "s " : { date.setSeconds(date.getSeconds()+number); return date; break; } default : { date.setDate(d.getDate()+number); return date; break; } } } var now = new Date(); //加五天. var newDate = DateAdd( "d ",5,now); alert(newDate.toLocaleDateString()) //加两个月. newDate = DateAdd( "m ",2,now); alert(newDate.toLocaleDateString()) //加一年 newDate = DateAdd( "y ",1,now); alert(newDate.toLocaleDateString())
。