0
votes

Generate all dates of a week using current date also show week starting date on Monday and weekends on Sunday using JavaScript or AngularJS.

  1. User can enter any date (any year and month)
  2. Week Should start on Monday and end on Sunday
  3. It should work of all years & months

Ex: Selected date ( Friday 1st Jan 2016) (default it should select Current date) strong text

It should Show 28/12/2015(monday) 29/12/2015(tuesday) 30/12/2015(wednesday) 31/12/2015(thursday) 1/1/2016(friday) 2/1/2016(saturday) 3/1/2016(sunday)

$('#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
Can you post the code you have written pls.Jax
What is your question?Sina Gh
Welcome to SO Vikas. Have a look at Date.prototype.getDay(), try writing some code and let us know if you get stuck. If you don't need further answers you can answer your own question or delete it.traktor
here is the code I have... Not working for some days (ex: 02 jan 2016) link to my codeVikas G Reddy
You seem to be using toLocaleDateString('en-US'); whereas the question seems to require en-GB. This is also dependent on support for the Intl object, so will fail in browsers that don't (Safari). Coping a date by converting to a string, then parsing the string is horrible, use something like new Date(+date) where date is a Date object.RobG

1 Answers

1
votes
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);