JavaScript for getting the previous Monday

后端 未结 7 2122
孤城傲影
孤城傲影 2021-02-19 04:03

I would like for the previous Monday to appear in the field where a user enters today\'s date.

E.g.: If today\'s date is entered 29-Jan-16 t

相关标签:
7条回答
  • 2021-02-19 04:43

    Here is a fiddle demonstrating a few different formats: https://jsfiddle.net/umefez2j/3/

    function getMonday(d) {
      var m_names = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
      var d = new Date(d);
      var day = d.getDay(),
          diff = d.getDate() - day + (day == 0 ? -6:1); // adjust when day is sunday
    
      //Use this one to return this format: Mon Jan 25 2016 09:37:51 GMT-0600 (Central Standard Time)
      //return new Date(d.setDate(diff));
    
      //Use this one to return this format: Mon, 25 Jan 2016
      //monday=new Date(d.setDate(diff)).toUTCString();
      //monday=monday.split(' ').slice(0, 4).join(' ')
      //return monday;
    
      //Use this one to return this format: 25-Jan-2016
      monday=new Date(d.setDate(diff));
      var curr_date = monday.getDate();
      var curr_month = monday.getMonth();
      var curr_year = monday.getFullYear();
      return curr_date + "-" + m_names[curr_month] + "-" + curr_year;
    }
    
    alert(getMonday(new Date()));
    
    //Created with help from:
    //http://stackoverflow.com/a/27480352/3112803
    //http://stackoverflow.com/a/4156516/3112803
    //http://stackoverflow.com/a/27869948/3112803
    
    0 讨论(0)
  • 2021-02-19 04:44

    Using moment.js:

    moment().day("Monday").format('YYYY-MM-DD');
    
    0 讨论(0)
  • 2021-02-19 04:52

    Based on @Philippe Dubé-Tremblay answer, i wanted to come up with something that lets you target any previous day:

    let target = 1 // Monday
    let date = new Date()
    date.setDate(date.getDate() - ( date.getDay() == target ? 7 : (date.getDay() + (7 - target)) % 7 ))
    

    This takes into account the previous Monday if today is also Monday

    0 讨论(0)
  • 2021-02-19 04:55

    I think your math is just a little off, and I tidied your syntax;

    function getPreviousMonday()
    {
        var date = new Date();
        var day = date.getDay();
        var prevMonday = new Date();
        if(date.getDay() == 0){
            prevMonday.setDate(date.getDate() - 7);
        }
        else{
            prevMonday.setDate(date.getDate() - (day-1));
        }
    
        return prevMonday;
    }
    

    That way you always get the last Monday that happened (which is 7 days ago if today is Monday)

    0 讨论(0)
  • 2021-02-19 04:57
    var prevMonday = new Date();
    prevMonday.setDate(prevMonday.getDate() - (prevMonday.getDay() + 6) % 7);
    

    I am a little late but I like this solution. It's a one liner and not that complicated. Making a function for that seems overkill to me.

    0 讨论(0)
  • 2021-02-19 05:05

    The easiest solution is to go back the number of days based on today's date. For example, if today's day(0) is Sunday, you can go back 6 days to find the previous Monday. If Today is Monday, you can go back 7 days. Therefore using a modulo will help you in this scenario simply because the number of days you will need to go back is %7 plus 6. Let me break it down in simple steps.

    var today=new Date();
    

    This will give you today's date. Now,

    var todaysDay=today.getDay();
    

    This will give you your day starting with zero.

    var goBack=today.getDay()%7+6;
    

    Now, declare a new date.

    var lastMonday=new Date().setDate(today.getDate()-goBack);
    

    Now, this will give you a numeric date value. Convert it back to Date

    var desiredDate=new Date(lastMonday);
    
    0 讨论(0)
提交回复
热议问题