欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  互联网

JQ日期与当天天数差计算

程序员文章站 2022-07-04 21:02:52
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 ......
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