Javascript中的日期差异(忽略一天的时间) [英] Date difference in Javascript (ignoring time of day)

查看:94
本文介绍了Javascript中的日期差异(忽略一天的时间)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个设备租赁申请,客户根据租金的持续时间(天)来收取租用设备的费用。所以,基本上,(每日费用*天数)=总费用。

I'm writing an equipment rental application where clients are charged a fee for renting equipment based on the duration (in days) of the rental. So, basically, (daily fee * number of days) = total charge.

对于客户端的即时反馈,我试图使用Javascript来计算出两个日历日期的差异。我已经搜索过,但我发现没有什么是我正在寻找的。我看到的大多数解决方案的格式如下:

For instant feedback on the client side, I'm trying to use Javascript to figure out the difference in two calendar dates. I've searched around, but nothing I've found is quite what I'm looking for. Most solutions I've seen are of the form:

function dateDiff1(startDate, endDate) {
    return ((endDate.getTime() - startDate.getTime()) / 1000*60*60*24);
}

我的问题是可以检查设备并在任何在这两个日期的时间段,不收取额外费用。上述代码是计算两个日期之间的24小时周期数,当我对日历日数感兴趣时。

My problem is that equipment can be checked out and returned at any time of day during those two dates with no additional charge. The above code is calculating the number of 24 hour periods between the two dates, when I'm really interested in the number of calendar days.

例如,如果有人检查在7月6日上午6点退出设备,并于7月7日晚上10点退回,上述代码将计算出超过一个24小时的时间过去,并返回2.所需结果为1,因为只有一个日历日期过去了即,第6到第7)

For example, if someone checked out equipment at 6am on July 6th and returned it at 10pm on July 7th, the above code would calculate that more than one 24 hour period had passed, and would return 2. The desired result is 1, since only one calendar date has elapsed (i.e. the 6th to the 7th).

我找到的最接近的解决方案是这个功能:

The closest solution I've found is this function:

function dateDiff2(startDate, endDate) {
    return endDate.getDate() - startDate.getDate();
}

这样做完全是我想要的,只要两个日期都在同一个月但是,由于getDate()只返回月份(即1-31),所以当日期跨越多个月时(例如7月31日至8月1日为1天,但上述计算为1 - 31,或-29)。

which does exactly what I want, as long as the two dates are within the same month. However, since getDate() only returns the day of month (i.e. 1-31), it doesn't work when the dates span multiple months (e.g. July 31 to August 1 is 1 day, but the above calcuates 1 - 31, or -29).

在后端,在PHP中,我使用gregoriantojd(),这似乎工作正常(参见这篇文章为例)。我只是在Javascript中找不到等同的解决方案。

On the backend, in PHP, I'm using gregoriantojd(), which seems to work just fine (see this post for an example). I just can't find an equivalent solution in Javascript.

任何人都有任何想法?

推荐答案

如果你想要整个学期的学生相机租赁示例...

If you want whole days for your student camera rental example ...

function daysBetween(first, second) {

    // Copy date parts of the timestamps, discarding the time parts.
    var one = new Date(first.getFullYear(), first.getMonth(), first.getDate());
    var two = new Date(second.getFullYear(), second.getMonth(), second.getDate());

    // Do the math.
    var millisecondsPerDay = 1000 * 60 * 60 * 24;
    var millisBetween = two.getTime() - one.getTime();
    var days = millisBetween / millisecondsPerDay;

    // Round down.
    return Math.floor(days);
}

这篇关于Javascript中的日期差异(忽略一天的时间)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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