计算两个日期相差的天数 js php日期 减一年
计算两个日期相差的天数
//sDate1和sDate2是yyyy-MM-dd格式 function dateDiff(sDate1, sDate2) { var aDate, oDate1, oDate2, iDays,iMonth; aDate = sDate1.split("-"); oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]); //转换为yyyy-MM-dd格式 aDate = sDate2.split("-"); oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]); iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24); //把相差的毫秒数转换为天数 iMonth = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24 /30); //把相差的毫秒数转换为月 return {d:iDays,m:iMonth}; //返回相差天数 } dateDiff('2017-02-02','2017-02-15');
//两个日期之间所有的日期
var getDates = function(startDate, endDate) { var dates = [], currentDate = startDate, addDays = function(days) { var date = new Date(this.valueOf()); date.setDate(date.getDate() + days); return date; }; while (currentDate <= endDate) { dates.push(currentDate); currentDate = addDays.call(currentDate, 1); } return dates; }; var dates = getDates(new Date(2017,12,22), new Date(2018,01,10)); dates.forEach(function(date) { console.log(date); });
日期减一年
var a =new Date('2017-10-12'); a.setFullYear(a.getFullYear() +1);
referr: https://stackoverflow.com/questions/33070428/add-year-to-todays-date
当然在php里面减一年就简单了
$date = '2004-02-29'; echo date('Y-m-d',strtotime('-1 year',strtotime($date)));