使用当前日期生成一周的所有日期,也显示星期一的星期开始日期和星期日的周末

时间:2016-01-14 11:02:16

标签: javascript angularjs date weekend

使用当前日期生成一周的所有日期,也可以使用JavaScript或AngularJS显示星期一的星期开始日期和星期日的周末。

  1. 用户可以输入任何日期(任何年份和月份)
  2. 周应该从周一开始,到周日结束
  3. 它应该适用于所有年份和个月
  4. 例如:选定日期(2016年1月1日星期五)(默认情况下应选择当前日期)强文

    它应该显示         28/12/2015(星期一)        29/12/2015(星期二)         30/12/2015(星期三)         31/12/2015(星期四)         2016年1月1日(星期五)         2016年2月1日(星期六)         2016年3月1日(星期日)

    $('#datepicker').datepicker({}).datepicker("setDate", new Date());
    
    
    window.onload = lastDate;
    
    function lastDate() {
      lDate = document.getElementById('datepicker').value;
    
      var myDate = new Date(lDate).toLocaleDateString('en-US');
      var xDate = moment(lDate).format("D-MMM-YYYY");
    
    
    
      var curr = new Date(myDate); 
      var sun = curr.getDate() - curr.getDay(); 
      var sat = sun + 6; 
      var mon = sun + 1;
      var tue = sun + 2;
      var wed = sun + 3;
      var thu = sun + 4;
      var fri = sun + 5;
      var sun1 = sun + 7;
      var last = sun1;
    
      sunday = new Date(curr.setDate(sun)).toLocaleDateString('en-US');
      monday = new Date(curr.setDate(mon)).toLocaleDateString('en-US');
      tuesday = new Date(curr.setDate(tue)).toLocaleDateString('en-US');
      wednesday = new Date(curr.setDate(wed)).toLocaleDateString('en-US');
      thursday = new Date(curr.setDate(thu)).toLocaleDateString('en-US');
      friday = new Date(curr.setDate(fri)).toLocaleDateString('en-US');
      saturday = new Date(curr.setDate(sat)).toLocaleDateString('en-US');
      sunday1 = new Date(curr.setDate(sun1)).toLocaleDateString('en-US');
      lastday1 = new Date(curr.setDate(sun1)).toLocaleDateString('en-US');
    
      document.getElementById("day1").innerHTML = sunday;
      document.getElementById("day2").innerHTML = monday;
      document.getElementById("day3").innerHTML = tuesday;
      document.getElementById("day4").innerHTML = wednesday;
      document.getElementById("day5").innerHTML = thursday;
      document.getElementById("day6").innerHTML = friday;
      document.getElementById("day7").innerHTML = saturday;
      document.getElementById("day1").innerHTML = sunday1;
    
    
    }
    
    
    
    
        <body>
      <div class="container">
        <h3>This week dates </h3>
        <input id="datepicker" class="currentDate" onchange="lastDate()">/input>
    
      </div>
      <div Class="container-fluid well">
        <div class="form-inline">
    
          <div class="form-control">
            <span class="label label-default">Monday</span>
            <span id="day2"> </span>
          </div>
          <div class="form-control">
            <span class="label label-default">Tuesday</span>
            <span id="day3"> </span>
          </div>
          <div class="form-control">
            <span class="label label-default">Wednesday</span>
            <span id="day4"> </span>
          </div>
          <div class="form-control">
            <span class="label label-default">Thursday</span>
            <span id="day5"> </span>
          </div>
          <div class="form-control ">
            <span class="label label-default">Friday</span>
            <span id="day6"> </span>
          </div>
          <div class="form-control">
            <span class="label label-default">Saturday</span>
            <span id="day7"> </span>
          </div>
          <div class="form-control">
            <span class="label label-default">Sunday</span>
            <span id="day1"></span>
          </div>
    
        </div>
      </div>
    
    
    </body>
    

1 个答案:

答案 0 :(得分:1)

var curr = new Date;
var Sunday = new Date(curr.setDate(curr.getDate() - curr.getDay()));
var Monday = new Date(curr.setDate(curr.getDate() - curr.getDay()+1));
var Tuesday = new Date(curr.setDate(curr.getDate() - curr.getDay()+2));
var Wednesday = new Date(curr.setDate(curr.getDate() - curr.getDay()+3));
var Thursday = new Date(curr.setDate(curr.getDate() - curr.getDay()+4));
var Friday = new Date(curr.setDate(curr.getDate() - curr.getDay()+5));
var Saturday = new Date(curr.setDate(curr.getDate() - curr.getDay()+6));


console.log(Sunday);
console.log(Monday);
console.log(Tuesday);
console.log(Wednesday);
console.log(Thursday);
console.log(Friday);
console.log(Saturday);