SoFunction
Updated on 2025-03-04

js get today and past date

1. Get the number of days, such as yesterday, today, nearly 1 week, and nearly 14 days

function GetDateStr(AddDayCount) { 
    var dd = new Date(); 
    (()+AddDayCount);//Get the date of AddDayCount diva    var y = (); 
    var m = ()+1;//Get the date of the current month    var d = (); 
    //Judge Month    if(m < 10){
      m = "0" + m;
    }else{
      m = m;
    }
    //Judgement Day n    if(d &lt; 10){//If the number of days is <10      d = "0" + d;
    }else{
      d = d;
    }
    return y+"-"+m+"-"+d; 
  } 

2. Get month, such as 1 month ago, 3 months ago, 6 months ago

function GetMonthStr(AddMonthCount) { 
    var dd = new Date(); 
    (()+AddMonthCount);//Get the date after AddMonthCount month    var y = (); 
    var m = ()+1;//Get the date of the current month    var d = (); 
    //Judge Month    if(m &lt; 10){
      m = "0" + m;
    }else{
      m = m;
    }
    //Judgement Day n    if(d &lt; 10){//If the number of days is <10      d = "0" + d;
    }else{
      d = d;
    }
    return y+"-"+m+"-"+d; 
  } 

3. Obtain the year as 1 year ago

function GetYearStr(AddYearCount) { 
    var dd = new Date(); 
    (()+AddYearCount);//Get the date after AddMonthCount month    var y = (); 
    var m = ()+1;//Get the date of the current month    var d = (); 
    //Judge Month    if(m &lt; 10){
      m = "0" + m;
    }else{
      m = m;
    }
    //Judgement Day n    if(d &lt; 10){//If the number of days is <10      d = "0" + d;
    }else{
      d = d;
    }
    return y+"-"+m+"-"+d; 
  }

I hope the above will be helpful to all friends!