JQuery UI. The difference of the date picker in days.

I need to calculate the difference of weeks between the selected date and the current date. I tried to calculate with weekNumberPicked - weekNumberCurrent , but if the two dates are in different years, the result is wrong, so I probably need to get it as daysDifference / 7 . How to implement this using the onSelect action?

+7
source share
3 answers

You can use the getdate Datepicker function to get a Date object.

Then simply subtract one date from another (you might also want to get the absolute value) to get the difference in milliseconds and calculate the difference in days or weeks.

 $('#test').datepicker({ onSelect: function() { var date = $(this).datepicker('getDate'); var today = new Date(); var dayDiff = Math.ceil((today - date) / (1000 * 60 * 60 * 24)); } }); 
+15
source

Since the DatePicker getDate () method returns a Date javascript object, you can do something like:

 var myDate = $('.datepicker').datepicker('getDate'); var current = new Date(); var difference = myDate - current; 

difference now contains the number of milliseconds between two dates, you can easily calculate the number of weeks:

 var weeks = difference / 1000 / 60 / 60 / 24 / 7; 
+2
source

try this code and apply it to your work: D

 $("#date_born").datepicker({ onSelect: function () { var start = $('#date_born').datepicker('getDate'); var end = new Date(); var age_year = Math.floor((end - start)/31536000000); var age_month = Math.floor(((end - start)% 31536000000)/2628000000); var age_day = Math.floor((((end - start)% 31536000000) % 2628000000)/86400000); $('#age').val(age_year +' year ' + age_month + ' month ' + age_day + ' day'); }, dateFormat: 'dd/mm/yy', maxDate: '+0d', yearRange: '1914:2014', buttonImageOnly: false, changeMonth: true, changeYear: true }); 

HTML code:

 Date <input type="text" name="date_born" id="date_born"/> Age <input type="text" name="age" id="age" /> 
0
source

All Articles