dsqa6272 2016-10-05 04:59 采纳率: 0%
浏览 1106

需要在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.

  • 写回答

4条回答 默认 最新

  • dongzhui9936 2016-10-05 05:10
    关注
    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法