简体   繁体   中英

Need to calculate months between two dates in JQuery

I need to count total no of months between the two dates in months and years if not just months either any php snippet or ether js or jquery.

I display the data from backend using php tags {{ $job->job_start_date }} job_start_date is for start date and {{ $job->job_expiry_date }}) job_expiry_date is for end date.

I get my output as in this format 13-Oct-2016. I tried this thing putting the values as hidden but couldn't get it work with parsing properly while adding new Date() to them

$(document).ready(function(){
    var sDate = $("#monthStart").val();
    var nDate = $("#monthEnd").val();

    var sd = new Date(sDate );
    var ed = new Date(nDate );
    ed.setDate(ed.getDate() - sd.getDate());
    alert(monthDiff(sd,ed));
});


function monthDiff(d1, d2) {
    var months;
    months = (d2.getFullYear() - d1.getFullYear()) * 12;
    months -= d1.getMonth() + 1;
    months += d2.getMonth();
    return months <= 0 ? 0 : months;
}

need a easy solution in js jquery or php.

function differenceInMonths($startDate, $endDate)
{
    $date1 = new DateTime($startDate);
    $date2 = new DateTime($endDate);

    $interval = date_diff($date1, $date2);

    return $interval->m + ($interval->y * 12) . ' months';
}

This way you get no. of months. Now you can calculate years if months >= 11.

Well in this case you can take advantage of moment.js library.

Assuming you have Date String in format like "1-Sept-2016" then we need to convert it to date and then apply moment. If you already have Date then you can directly apply the moment .

Demo :

 var startDate = new Date('01-Sept-2016' ); var endDate = new Date('30-Oct-2016' ); var endMoment = moment(endDate); var startMoment = moment(startDate); //[days, years, months, seconds, ...] console.log(endMoment.diff(startMoment, 'months'));
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.1/moment.js"></script>

You can even specify the difference you want like days , months , years , seconds as specified above.

Reference : http://momentjs.com/

function MonthDiff(date1, date2) {
    var Nomonths;
    Nomonths= (date2.getFullYear() - date1.getFullYear()) * 12;
    Nomonths-= date1.getMonth() + 1;
    Nomonths+= date2.getMonth() +1; // we should add + 1 to get correct month number
    return Nomonths <= 0 ? 0 : Nomonths;
}

Here is a php solution:

$noMonth = 1 + (date('Y',strtotime($job->job_expiry_date)) - date('Y',strtotime($job->job_start_date))) * 12   +   (date('m',strtotime($job->job_expiry_date)) - date('m',strtotime($job->job_start_date)))

The first part calculates the difference in years, multiplied by 12 results the number of month:

1 + (date('Y',strtotime($job->job_expiry_date)) - date('Y',strtotime($job->job_start_date))) * 12

The second part calculates the number of months to be added or subtracted, as if the dates were in the same year:

(date('m',strtotime($job->job_expiry_date)) - date('m',strtotime($job->job_start_date)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM