zoukankan      html  css  js  c++  java
  • js计算两日期相差的天数、月数

    //返回两个日期相差的月数
    function MonthsBetw(date1, date2) { //date1和date2是2019-3-12格式
    //用-分成数组
    date1 = date1.split("-");
    date2 = date2.split("-");
    //获取年,月数
    var year1 = parseInt(date1[0]),
    month1 = parseInt(date1[1]),
    year2 = parseInt(date2[0]),
    month2 = parseInt(date2[1]),
    //通过年,月差计算月份差
    months = (year2 - year1) * 12 + (month2 - month1) + 1;
    return months;
    }

    //计算天数差的函数,通用
    function DateDiff(sDate1, sDate2) { //sDate1和sDate2是2019-3-12格式
    var aDate, oDate1, oDate2, iDays
    aDate = sDate1.split("-")
    oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]) //转换为9-25-2017格式
    aDate = sDate2.split("-")
    oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0])
    iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24) //把相差的毫秒数转换为天数
    return iDays
    }
  • 相关阅读:
    罗马数字
    逆序对
    等价串
    郊区春游
    贝壳找房函数最值
    Educational Codeforces Round 45 Editorial
    Codeforces Round #486 (Div. 3)
    checkbox保存和赋值
    oninput和onchange的区别
    cookie路径概念理解
  • 原文地址:https://www.cnblogs.com/zyybb/p/10521447.html
Copyright © 2011-2022 走看看