JQ日期与当天天数差计算

function getDateStr(obj){
      var s,y,m,d,str,a,b,a1,b1;
      s = new Date();
      y = s.getFullYear();
      m = s.getMonth()+1;
      d = s.getDate();
      if(m >= 1 && m <= 9) m = '0' + m;
      if(d >= 1 && d <= 9) d = '0' + d;
      str = y+'-'+m+'-'+d;

      a = str.split('-');
      b = obj.split('-');
      a1 = new Date(a[1]+'-'+a[2]+'-'+a[0]);
      b1 = new Date(b[1]+'-'+b[2]+'-'+b[0]);
      var Days = parseInt(b1-a1)/1000/60/60/24;
      //var Days = parseInt(Math.abs(a1-b1)/1000/60/60/24);
      return Days;
}

示例


function getDateStr(obj) { 
    var s, y, m, d, str, a, b, a1, b1;
    s = new Date();
    y = s.getFullYear();
    m = s.getMonth() + 1;
    d = s.getDate();
    if (m >= 1 && m <= 9) m = '0' + m;
    if (d >= 1 && d <= 9) d = '0' + d;
    str = y + '-' + m + '-' + d;

    a = str.split('-');
    b = obj.split('/');
    a1 = new Date(a[1] + '-' + a[2] + '-' + a[0]);
    b1 = new Date(b[1] + '-' + b[2] + '-' + b[0]);
    var Days = parseInt(b1 - a1) / 1000 / 60 / 60 / 24;
    //var Days = parseInt(Math.abs(a1-b1)/1000/60/60/24);
    return Days;
}

var d = new Date("2020-09-11 03:14:34.000")
d.setMonth(d.getMonth() + 3);
var new_validitydate = d.toLocaleString().substring(0, 10);      
console.log(new_validitydate);

var days = getDateStr(new_validitydate);
console.log(days);

本文地址:https://blog.csdn.net/weixin_42388648/article/details/101055715

(0)
上一篇 2022年3月22日
下一篇 2022年3月22日

相关推荐