如何使用Moment JS计算范围之间的给定工作日数? [英] How do I calculate number of given weekday between range using Moment JS?

查看:648
本文介绍了如何使用Moment JS计算范围之间的给定工作日数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Moment JS和jQuery找出给定工作日中的几个(例如:星期二)在日期范围内?

我可以使用差值找到天数: http://momentjs.com/docs/#/displaying/difference/

我知道开始和结束日期,所以我觉得应该可以吗?

示例:3月1日至3月25日之间有几个星期二?

解决方案

我想出了以下功能,该功能似乎适用于我尝试过的少数测试用例:

function weekdaysBetween(d1, d2, isoWeekday) {
    // ensure we have valid moment instances
    d1 = moment(d1);
    d2 = moment(d2);
    // figure out how many days to advance to get to the next
    // specified weekday (might be 0 if d1 is already the 
    // specified weekday).
    var daysToAdd = ((7 + isoWeekday) - d1.isoWeekday()) % 7;
    var nextTuesday = d1.clone().add(daysToAdd, 'days');
    // if we are already passed the end date, there must not
    // be any of that day in the given period.
    if (nextTuesday.isAfter(d2)) {
        return 0;
    }
    // otherwise, just return the whole number of weeks
    // difference plus one for the day we already advanced to
    var weeksBetween = d2.diff(nextTuesday, 'weeks');
    return weeksBetween + 1;
}

您将当天的 isoWeekday 值传递您正在尝试数数.例如在星期二,请输入2.

示例调用:

var d1 = moment('2015-03-01');
var d2 = moment('2015-03-25');

console.log('result:', weekdaysBetween(d1, d2, 2)); // => result: 4

Wolfram Alpha给出相同的结果.

但是,在完全信任它之前,您应该添加自己的测试.

How do I find out how many of a given weekday (ex: Tuesdays) are in a date range using Moment JS and jQuery?

I can find the number of days using Difference: http://momentjs.com/docs/#/displaying/difference/

And I know the start and end date, so I feel like it should be possible?

Example: How many Tuesdays are there between March 1st and March 25th?

解决方案

I came up with the following function that seems to work for the given handful of test cases I tried:

function weekdaysBetween(d1, d2, isoWeekday) {
    // ensure we have valid moment instances
    d1 = moment(d1);
    d2 = moment(d2);
    // figure out how many days to advance to get to the next
    // specified weekday (might be 0 if d1 is already the 
    // specified weekday).
    var daysToAdd = ((7 + isoWeekday) - d1.isoWeekday()) % 7;
    var nextTuesday = d1.clone().add(daysToAdd, 'days');
    // if we are already passed the end date, there must not
    // be any of that day in the given period.
    if (nextTuesday.isAfter(d2)) {
        return 0;
    }
    // otherwise, just return the whole number of weeks
    // difference plus one for the day we already advanced to
    var weeksBetween = d2.diff(nextTuesday, 'weeks');
    return weeksBetween + 1;
}

You pass in the isoWeekday value for the day you are trying to count. e.g. for Tuesday, pass in 2.

Sample invocation:

var d1 = moment('2015-03-01');
var d2 = moment('2015-03-25');

console.log('result:', weekdaysBetween(d1, d2, 2)); // => result: 4

Wolfram Alpha gives the same result.

You should add your own tests before trusting this completely, though.

这篇关于如何使用Moment JS计算范围之间的给定工作日数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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