Thomas Niederberger
Thomas Niederberger

Reputation: 1

Calculating <input type="date" with jQuery

I'm struggling with the idea to display the amount of days between the selected date choosen by: <input type="date" id="date" /> and today.

Based on that I would like to show a specific <div>. For example if there are 20 days between the date from the input value and today we show <div id="20day"></div> and if there are more then 20 days we show <div id="21day"></div>.

Is this possible just with jQuery?

Upvotes: 0

Views: 1498

Answers (2)

palaѕн
palaѕн

Reputation: 73896

Try this:

$('#date').change(function () {
    var date1 = new Date($(this).val());
    var date2 = new Date();
    var diffDays = date2.getDate() - date1.getDate();
    $('body').append('<div id="' + diffDays + '"></div>');
});

DEMO HERE

Upvotes: 2

Techie
Techie

Reputation: 45124

var selected = $('#date').val();
var today = new Date();

var diff = new Date(selected - today);

// get days
var days = diff/1000/60/60/24;

alert(days);

var days will contains the number of days. You can go ahead and do whatever you like with the variable. I just used alert() here.

Upvotes: 0

Related Questions