Hi guys,

I try to calculate number of month between 2 selected dates and i just got this code. The problem is this code calculate number of days and i try change the code from

var days = (end - start) / (1000 * 60 * 60 *24) ;
to
var days = (end - start) / (60 * 60 * 24 * 30);

Here the full code :

javascript:

$("#TextBox1").datepicker({
    minDate: 0,
    maxDate: '+1Y+6M',
    onSelect: function (dateStr) {
        var min = $(this).datepicker('getDate'); // Get selected date
        $("#TextBox2").datepicker('option', 'minDate', min || '0'); // Set other min, default to today
    }
});


$("#TextBox2").datepicker({
    minDate: '0',
    maxDate: '+1Y+6M',
    onSelect: function (dateStr) {
        var max = $(this).datepicker('getDate'); // Get selected date
        $('#datepicker').datepicker('option', 'maxDate', max || '+1Y+6M'); // Set other max, default to +18 months
        var start = $("#TextBox1").datepicker("getDate");
        var end = $("#TextBox2").datepicker("getDate");
        var days = (end - start) / (1000 * 60 * 60 *24);

        $("#TextBox3").val(days);
    }
});

html :

<p>Date From :
    <input type="text" id="TextBox1" /> </p> <p>Date To :
    <input type="text" id="TextBox2" /> </p> <p>Difference :
    <input type="text" id="TextBox3" /> </p>

i put 01-01-2015 to 31-01-2015 and the result is 1000 month. I dont know why the code make it 1000. Any advise and help from you guys?.....

Recommended Answers

All 9 Replies

Member Avatar for diafol

Your question is confusion - probably why nobody has replied yet.

The problem is this code calculate number of days and i try change the code from
var days = (end - start) / (1000 * 60 * 60 *24) ;
to
var days = (end - start) / (60 * 60 * 24 * 30);

So are you saying it worked before you changed it to the second example? Maybe you're not asking for js getDate but some property of the datepicker - but I don't know which one you're using.

Javascript getDate() gets the day date - not seconds since UNIX epoch. What are you expecting?

oh sorry, i forgot to change the second var days. it supposed to be var month.
var days = (end - start) / (1000 * 60 * 60 *24) ;
to
var month = (end - start) / (60 * 60 * 24 * 30);

i changed var days formula to var month formula because i want to calculate those days to month only. But the result i get is 1000 month instead 1 month. i put 01-01-2015 for Textbox1 and 31-01-2015 for Textbox2.

Member Avatar for diafol

Which datepicker are you using jqueryui?

Thank mr diafol. that really useful for me...

Hi, i have the same problem too, just i not using jqueryui datepicker. I use default html calendar. Here is my code :

<input name="d_start"  type=text id="d_start" value="<?php echo $_POST['start']; ?>" SIZE="20" maxlength="10">
<input name="kalendar12" type= "image" id="kalendar12" onClick="popUpCalendar(this, this.form.d_start,'dd-mm-yyyy');return false;" 
src="images/calendar.gif" alt="kalendar" />
<span class="style1">(hh-bb-tttt)</span>

Any way to help me? Plz....

Member Avatar for diafol

You're not using default HTML calendar from what I can see - you are using a popUpCalendar script. Your HTML is rather messed up though. No quotes around text.

Sorry, i still new in this php/html world. Im using the same javascript and popupcalendar for my calendar.
Here my full codes :

javascript :

$("#d_start").datepicker({
minDate: 0,
maxDate: '+1Y+6M',
onSelect: function (dateStr) {
var min = $(this).datepicker('getDate'); // Get selected date
$("#d_end").datepicker('option', 'minDate', min || '0'); // Set other min, default to today
}
});

$("#d_end").datepicker({
minDate: '0',
maxDate: '+1Y+6M',
onSelect: function (dateStr) {
var max = $(this).datepicker('getDate'); // Get selected date
$('#datepicker').datepicker('option', 'maxDate', max || '+1Y+6M'); // Set other max, default to +18 months
var start = $("#d_start").datepicker("getDate");
var end = $("#d_end").datepicker("getDate");
var days = (end - start) / (1000 * 60 * 60 *24);
$("#month").val(days);
}
});

html:

<tr>
<td><span class="style1">Date Start<span class="style5">*</span></span></td>
<td><span class="style1">:</span></td>
<td><input name="d_start"  type="text" id="d_start" size="20" maxlength="10">
<input name="kalendar12" type= "image" id="kalendar12" onClick="popUpCalendar(this, this.form.d_start,'dd-mm-yyyy');return false;" 
src="images/calendar.gif" alt="kalendar" />
<span class="style1">(hh-bb-tttt)</span></td>
</tr>

<tr>
<td><span class="style1">Date End<span class="style5">*</span></span></td>
<td><span class="style1">:</span></td>
<td><input name="d_end"  type="text" id="d_end" size="20" maxlength="10">
<input name="kalendar13" type= "image" id="kalendar13" onClick="popUpCalendar(this, this.form.d_end,'dd-mm-yyyy');return false;" 
src="images/calendar.gif" alt="kalendar" />
<span class="style1">(hh-bb-tttt)</span></td>
</tr>

<tr>
    <td> Total Month :
    <input type="text" id="month"/>
    </td>
</tr>

I've tried using your code and the total month still not working. I guess there's a problem with my calendar since you using datepicker. Should i change the onSelect code from datepicker to popupcalendar. Any proper solution for this?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.