1

I am stuck with the automatic calculation of days and automatic insertion of month, calendar week based on the user entered data.

I searched and I got to calculate the differences only by static not by the user entered dates.

I have 6 Text boxes.

  • 1st textbox "Year" user enters a specific date (dd.mm.yy)
  • 2nd text box needs to be automatically filled with the respective calendar week
  • 3rd text box needs to be automatically filled with the respective month (Jan, Feb, Mar)
  • 4th text box needs to be automatically filled with the respective year

  • 5th text box user enters another date (dd.mm.yy)

  • 6th text box needs to be automatically filled with the number of days difference between 2nd entered date and first entered date.

My code is as below.

$(function() { 
$( "#datepicker").datepicker({ dateFormat: 'dd.mm.yy' }); 
$("#datepickerend").datepicker({ dateFormat: 'dd.mm.yy'});
  
});
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<div>
<table border="0">
<tr>
<td>Date: <input type=text name="date" id="datepicker"></td>
</tr>
<tr>
<td>CW: <input type=text name="CW"></td>
</tr>
<tr>
<td>Month: <input type=text name="Month"></td>
</tr>
<tr>
<td>Year: <input type=text name="Year"></td>
</tr>
<tr>
<td>Ending Date: <input type=text name="enddate" id="datepickerend"></td>
</tr>
<tr>
<td>Days: <input type=text name="days"></td>
</tr>
</table>
</div>
halfer
  • 19,824
  • 17
  • 99
  • 186
John
  • 565
  • 8
  • 23

1 Answers1

0

You can used the onSelect event and at this time you can do whatever you want with the selected date.

$(function() {
$( "#datepicker").datepicker({
 dateFormat: 'dd.mm.yy',
 onSelect: function(date) {
    var selectedDate = $(this).datepicker('getDate');
    alert(selectedDate);

    // calculate week from datetime object from here
    // https://stackoverflow.com/questions/6117814/get-week-of-year-in-javascript-like-in-php
   }
});
$("#datepickerend").datepicker({ dateFormat: 'dd.mm.yy'});

});
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<div>
<table border="0">
<tr>
<td>Date: <input type=text name="date" id="datepicker"></td>
</tr>
<tr>
<td>CW: <input type=text name="CW"></td>
</tr>
<tr>
<td>Month: <input type=text name="Month"></td>
</tr>
<tr>
<td>Year: <input type=text name="Year"></td>
</tr>
<tr>
<td>Ending Date: <input type=text name="enddate" id="datepickerend"></td>
</tr>
<tr>
<td>Days: <input type=text name="days"></td>
</tr>
</table>
</div>
Du D.
  • 5,062
  • 2
  • 29
  • 34