This question is related to this SO question, and the provided solution is working fine. However, in my case, I want to get dates of mondays in a selected month from dropdown menu.
The code what I have tried is:
var d = new Date(),
month = d.getMonth(),
mondays = [],
mondaysChange = [];
//console.log("current month is: "+month);
//console.log("current months mondays are: " + getMondays());
$("#ddlMonths option").filter(function() {
return this.value == month;
}).attr('selected', true);
$('#ddlMonths').on('change', function(e) {
var m = $("option:selected", this).val();
console.log("selected month: " + m);
var str = getMondaysChange(m);
console.log("selected months mondays are: " + str);
});
function getMondaysChange(m) {
d.setDate(1);
d.setMonth(m);
console.log("you have selected: " + d + " month");
while (d.getDay() !== 1) {
d.setDate(d.getDate() + 1);
}
// Get all the other Mondays in the month
while (d.getMonth() === m) {
mondaysChange.push(new Date(d.getTime()));
d.setDate(d.getDate() + 7);
}
return mondaysChange;
}
function getMondays() {
d.setDate(1);
while (d.getDay() !== 1) {
d.setDate(d.getDate() + 1);
}
// Get all the other Mondays in the month
while (d.getMonth() === month) {
mondays.push(new Date(d.getTime()));
d.setDate(d.getDate() + 7);
}
return mondays;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment-with-locales.min.js"></script>
<div class="data-block">
<div class="data-container">
<header>
<h2>
Track</h2>
</header>
<div class="form-horizontal">
<div class="form-group">
<label for="CAASRTUserName" class="col-sm-2 control-label">Resource</label>
<div class="col-sm-10">
<input id="CAASRTUserName" type="text" data-bind="value: Vendor" placeholder="" class="form-control hasclear">
</div>
</div>
<div class="form-group has-feedback">
<label for="InvoiceTotal" class="col-sm-2 control-label">Enter for Month</label>
<div class="col-sm-10">
<select id="ddlMonths" class="form-control">
<option value="0">January</option>
<option value="1">February</option>
<option value="2">March</option>
<option value="3">April</option>
<option value="4">May</option>
<option value="5">June</option>
<option value="6">July</option>
<option value="7">August</option>
<option value="8">September</option>
<option value="9">October</option>
<option value="10">November</option>
<option value="11">December</option>
</select>
</div>
</div>
<!-- <div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button id="btnTrack" data-bind="click: Submit" class="btn btn-default">
Submit
</button>
</div>
</div> --></div>
<br>
<div class="row">
<div class="col-md-12">
<!-- <h4>
<span id="mySpanCur">Current Week</span>
</h4> -->
<div id="divtblTrack" class="plan">
<table id="tblTrack" class="table table-bordered">
<thead>
<tr>
<th>Parent Activity</th>
<th>Hrs/ Wk</th>
<th>Activity</th>
<th><span id="week1"></span>
</th>
<th><span id="week2"></span>
</th>
<th><span id="week3"></span>
</th>
<th><span id="week4"></span>
</th>
<th>Comments</th>
<th>Total</th>
<th style="display:none">PlanEntryId</th>
</tr>
</thead>
<tfoot>
<tr>
<td></td>
<td id="HrsByWkSum">204</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td id="TotalSum">0</td>
<td style="display:none"></td>
</tr>
</tfoot>
<tbody>
<tr>
<td class="emptyTd"><span>Analytics</span>
</td>
<td class="hoursbyweek emptyTd"><span>67</span>
</td>
<td class="DD">
<select class="form-control" readonly="readonly">
<option value="Select">Select</option>
</select>
</td>
<td class="mon">
<input type="text" id="txtMon" class="form-control" style="width:40px;" value="" />
</td>
<td class="tue">
<input type="text" id="txtTue" class="form-control" style="width:40px;" value="" />
</td>
<td class="wed">
<input type="text" id="txtWed" class="form-control" style="width:40px;" value="" />
</td>
<td class="thu">
<input type="text" id="txtThu" class="form-control" style="width:40px;" value="">
</td>
<td>
<input type="text" id="txtComments" class="form-control" style="width:90px;" value="">
</td>
<td class="tot emptyTdTot"></td>
<td style="display:none">3</td>
<td>
<button class="btn btn-primary btn-xs myBtn">Add</button>
</td>
</tr>
<tr>
<td class="emptyTd"><span>General Admin</span>
</td>
<td class="hoursbyweek emptyTd"><span>36</span>
</td>
<td class="DD">
<select class="form-control" readonly="readonly">
<option value="Select">Select</option>
</select>
</td>
<td class="mon">
<input type="text" id="txtMon" class="form-control" style="width:40px;" value="">
</td>
<td class="tue">
<input type="text" id="txtTue" class="form-control" style="width:40px;" value="">
</td>
<td class="wed">
<input type="text" id="txtWed" class="form-control" style="width:40px;" value="">
</td>
<td class="thu">
<input type="text" id="txtThu" class="form-control" style="width:40px;" value="">
</td>
<td>
<input type="text" id="txtComments" class="form-control" style="width:90px;" value="">
</td>
<td class="tot emptyTdTot"></td>
<td style="display:none">2</td>
<td>
<button class="btn btn-primary btn-xs myBtn">Add</button>
</td>
</tr>
<tr>
<td class="emptyTd"><span>Reports</span>
</td>
<td class="hoursbyweek emptyTd"><span>56</span>
</td>
<td class="DD">
<select class="form-control" readonly="readonly">
<option value="Select">Select</option>
</select>
</td>
<td class="mon">
<input type="text" id="txtMon" class="form-control" style="width:40px;" value="">
</td>
<td class="tue">
<input type="text" id="txtTue" class="form-control" style="width:40px;" value="">
</td>
<td class="wed">
<input type="text" id="txtWed" class="form-control" style="width:40px;" value="">
</td>
<td class="thu">
<input type="text" id="txtThu" class="form-control" style="width:40px;" value="">
</td>
<td>
<input type="text" id="txtComments" class="form-control" style="width:90px;" value="">
</td>
<td class="tot emptyTdTot"></td>
<td style="display:none">6</td>
<td>
<button class="btn btn-primary btn-xs myBtn">Add</button>
</td>
</tr>
<tr>
<td class="emptyTd"><span>Time not working</span>
</td>
<td class="hoursbyweek emptyTd"><span>45</span>
</td>
<td class="DD">
<select class="form-control" readonly="readonly">
<option value="Select">Select</option>
</select>
</td>
<td class="mon">
<input type="text" id="txtMon" class="form-control" style="width:40px;" value="">
</td>
<td class="tue">
<input type="text" id="txtTue" class="form-control" style="width:40px;" value="">
</td>
<td class="wed">
<input type="text" id="txtWed" class="form-control" style="width:40px;" value="">
</td>
<td class="thu">
<input type="text" id="txtThu" class="form-control" style="width:40px;" value="">
</td>
<td>
<input type="text" id="txtComments" class="form-control" style="width:90px;" value="">
</td>
<td class="tot emptyTdTot"></td>
<td style="display:none">1</td>
<td>
<button class="btn btn-primary btn-xs myBtn">Add</button>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<div class="pull-right">
<button id="btnSubmit" data-bind="click: Submit" class="btn btn-default">Save</button>
</div>
</div>
</div>
</div>
<br>
</div>
</div>
Jsbin for the same is here. I am setting the month on change of dropdown and am still wondering why the mondaysChange
array is returning empty. Can anyone check it?
A momentjs solution for the same also fine which I can't seem to get it easily.