Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery UI Datepicker difference in days

I need to calculate the number of weeks difference between the chosen date and the current date. I've tried to calculate with weekNumberPicked - weekNumberCurrent, but if the two dates are in different years, the result is incorrect, so I probably need to get it like daysDifference / 7. How should I implement this with the onSelect action?

like image 960
Petya petrov Avatar asked Feb 28 '11 13:02

Petya petrov


2 Answers

You can use the Datepicker's function getDate to get a Date object.

Then just subtract one date from the other (might want to get the absolute value as well) to get the milliseconds in difference, 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));
    }
});
like image 156
Joakim Johansson Avatar answered Oct 12 '22 04:10

Joakim Johansson


Since DatePicker getDate() methode returns a javascript Date 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 your two dates , you can easily compute the number of weeks :

var weeks = difference / 1000 / 60 / 60 / 24 / 7;
like image 36
krtek Avatar answered Oct 12 '22 03:10

krtek