zoukankan      html  css  js  c++  java
  • 计算两个日期相差的天数 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)));
    
  • 相关阅读:
    多线程 信号量
    sql在不同数据库查询前几条数据
    Office Outlook同步 很奇怪的BUG
    搜索小技巧整理
    想做一个权限管理插件
    ibatis和Castle学习历程
    查找存储过程中的错误位置
    VS2005项目模版丢失解决方案及VS2005项目模版查找原理
    C# 邮件发送接收
    数据库优化整合
  • 原文地址:https://www.cnblogs.com/tonnytong/p/8259767.html
Copyright © 2011-2022 走看看