使用jquery以(mm / dd / yyyy)格式获取两个日期之间的差异 [英] Get the difference in months between two dates in (mm/dd/yyyy) format using jquery

查看:151
本文介绍了使用jquery以(mm / dd / yyyy)格式获取两个日期之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要检索用户使用引导日期选择器输入的两个日期之间的月份差异。除此之外,我还试图将第二个值(date2)替换为用户单击计算按钮时月份的差异。当试图在这工作,我的代码似乎并没有工作......



这是我到目前为止:
$ b $($#$ $ $ $''$ click $')。click $('#loanTrm');
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffMonths = Math.ceil(timeDiff /(1000 * 3600 * 24));
date2.val(date2.val()。replace(diffMonths));
});

下面是我试图完成的图片演示:





我试图把这两个条目的差异(到期日 - 第一付款日期)




我想要让用户点击计算按钮后进行计算,月中的差额会被到期日输入的价值所取代。



任何帮助都将不胜感激!

解决方案

人类日历不利于简单的数学运算 - 幸运的是,javascript提供了一个Date对象,它可以为我们完成大部分的肮脏工作。

这会将日期字符串转换为Date对象(对输入字段内容的任何格式或验证问题进行修饰)。请注意,在特定情况下,由于您打算替换 #loanTrm 的值以月份为单位而不是日期,如果用户点击计算两次,则会中断此值;更正是更多用户界面问题一个编码问题,所以我暂时忽略它):

  var date1 = new Date($('#firstPayDate ).VAL()); 
var date2 = new Date($('#loanTrm')。val());
var datediff = date2 - date1;

(如下面的注释中所指出的,还要注意Date使用用户的区域设置来确定预期的日期格式:如果您始终以mm / dd / yy显示日期,那么您可能不应这样做,因为世界上大部分地区都使用dd / mm / yy。您需要基于也可以使用更明确的新日期(年,月,日,小时,分钟,秒,毫秒)而不是字符串快捷键I来构建日期)(就像我说的:日期很难!等到你尝试时区,它们更有趣)

datediff现在包含实际差异(以毫秒为单位) )

接下来发生的情况取决于月份差异的确切含义,因为月份并不总是相同的长度。



如果你不关心精确度,假设每个月都有三十天,除以 datediff,你可以得到一个可能接近的近似值(1000 * 60 * 60 * 24 * 30)。如果你关心精度,你已经摆脱了自己的领域,可能应该使用专门用于这种数学的一个库(momentjs是一个很好的例子)。



(还有很多其他的SO问题涉及这种类型的东西;任何依赖于字符串匹配而不是Date()的答案都会至少每四年出错一次,更常见的是。)


I was wanting to retrieve the month differences between two dates that a user enters in using the bootstrap datepicker. Along with this I am also trying to replace the second value (date2) to what the difference in months is when the user clicks the calculate button. When trying to work on this my code does not seem to be working...

Here is what I have so far:

$('#clickMe').click(function() {
    var date1 = $('#firstPayDate');
    var date2 = $('#loanTrm');
    var timeDiff = Math.abs(date2.getTime() - date1.getTime());
    var diffMonths = Math.ceil(timeDiff / (1000 * 3600 * 24)); 
    date2.val(date2.val().replace(diffMonths));
});

Here is a picture demonstration of what I am trying to accomplish:

I am trying to take the difference between these two entries (Maturity Date - First payment date)

I am trying to get the calculations to occur once the user clicks the Calculate button, the difference in months gets replaced with the value that was entered in Maturity Date.

Any help will be greatly appreciated!

解决方案

The human calendar is not conducive to easy math -- fortunately javascript provides a Date object which does most of the dirty work for us.

This will convert the date strings into Date objects (glossing over any formatting or validation issues with the input fields' contents. Note in your particular case that since you intend to replace #loanTrm's value with a number of months instead of a date, this will break if the user hits "calculate" twice; correcting that is more of a user interface issue than a coding issue so I'll ignore it for the time being):

var date1 = new Date($('#firstPayDate').val());
var date2 = new Date($('#loanTrm').val());
var datediff = date2 - date1; 

(As pointed out in comments below, note also that Date uses the user's locale to determine the expected date format: if you're always displaying dates in mm/dd/yy, well, you probably shouldn't do that, because much of the world uses dd/mm/yy. You'll either need to format the input field based on user locale as well, or else construct your Dates with the more explicit new Date(year, month, day, hours, minutes, seconds, milliseconds) instead of the string shortcut I showed above.) (Like I said: dates are hard! Wait until you try timezones, they're even more fun)

datediff now contains the real difference (in milliseconds) between the two dates.

What happens next depends on what you exactly mean by "the difference in months", because months aren't always the same length.

If you don't care about precision, you can get a probably-close-enough approximation by pretending every month has thirty days and dividing datediff by (1000*60*60*24*30). If you do care about precision, you've moved out of roll-your-own territory and probably ought to use one of the libraries devoted to this kind of math (momentjs is a good one.)

(There are many other SO questions covering this type of thing; any answer in any of them that depends on string matching instead of Date() is going to be wrong at least once every four years, and probably much more often that.)

这篇关于使用jquery以(mm / dd / yyyy)格式获取两个日期之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆