gpt4 book ai didi

javascript - 需要计算 JQuery 中两个日期之间的月份

转载 作者:行者123 更新时间:2023-11-30 09:43:47 25 4
gpt4 key购买 nike

我需要计算两个日期之间的总月数,以月和年为单位,如果不是任何 php 代码段或以太 js 或 jquery 的月份。

我使用 php 标签显示来自后端的数据 {{ $job->job_start_date }} job_start_date 是开始日期,{{ $job->job_expiry_date }}) job_expiry_date 是结束日期。

我在 2016 年 10 月 13 日得到了这种格式的输出。我试过将值隐藏起来,但是在向它们添加新的 Date() 时无法正确解析它

$(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;
}

在 js jquery 或 php 中需要一个简单的解决方案。

最佳答案

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;
}

关于javascript - 需要计算 JQuery 中两个日期之间的月份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39865748/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com