moment.js - 检查两个时刻是否来自同一周,但周是否从周五开始到周四结束

moment.js - Check if two moments are from the same week, but weeks begin on Friday and end on Thursday

我正在使用 node.js 和 discord.js 创建一个 Discord 机器人,并且有一个功能允许用户通过命令进行投票,但我希望他们每周只投票一次.

问题是,在这个 Discord 上,周从周五开始到周四结束,因此我不能简单地写:

var weekNow = moment().week();
var weekLastVote = moment(dateLastVote).week();
if (weekNow == weekLastVote){
    //Prevent from voting again
} else {
    //Let the user vote
}

因此,我编写了一些看起来可行的代码,但我想听听您的意见,因为它看起来很草率,而且我不确定我是否考虑了所有的可能性(我不不知道我是否需要使用我的 month 变量,例如:

module.exports = {
isSameWeek: function (dateLastVote) {
    // moments for today's date
    var dayNow = moment().weekday();
    var weekNow = moment().week();
    var monthNow = moment().month();
    var yearNow = moment().year();
    var dateNow = moment().format('MMDDYYYY'); // moment without hours/minutes/seconds

    // moments for last vote's date
    var dayLastVote = moment(dateLastVote).weekday();
    var weekLastVote = moment(dateLastVote).week();
    var monthLastVote = moment(dateLastVote).month();
    var yearLastVote = moment(dateLastVote).year();
    var dateLastVote = moment(dateLastVote).format('MMDDYYYY'); // moment without hours/minutes/seconds

    if ((yearNow === yearLastVote && weekNow === weekLastVote && dayLastVote < 5) || // 5 = Friday, starting day of the week (a week = Friday to thursday)
        (yearNow === yearLastVote && weekNow - 1 === weekLastVote && dayLastVote >= 5 && dayNow < 5) || 
        (dateNow === dateLastVote)
    ){
        return true;
    } else {
        return false;
    }
}

};

正如我所说,这似乎可以解决问题,但我希望其他人对此发表意见,以确保没有更简单的方法,或者,如果没有,如果我没有忘记任何事情。

感谢您的阅读:)

我不知道我们的方法在性能方面如何相互比较,但我仍然想展示我在这个问题上的方法:

function isSameWeek(firstDay, secondDay, offset) {
    var firstMoment = moment(firstDay);
    var secondMoment = moment(secondDay);

    var startOfWeek = function (_moment, _offset) {
        return _moment.add("days", _moment.weekday() * -1 + (_moment.weekday() >= 7 + _offset ? 7 + _offset : _offset));
    }

    return startOfWeek(firstMoment, offset).isSame(startOfWeek(secondMoment, offset), "day");
}

解决方案的作用是计算每个给定日期相对于偏移量的一周开始时间(对于值 >= -7 和 <= 0)并返回两者是否具有相同的一周开始时间.同一周的开始 = 同一周。

您所要做的就是调用传递两个日期对象(或时刻对象)和 -7 和 0 之间的偏移量的函数,具体取决于周相对于 "regular" 周的移动方式。

根据 Moment 文档,您可以设置一周的 ISO 开始:

moment().isoWeekday(1); // Monday
moment().isoWeekday(7); // Sunday

然后您可以使用相同的功能来检查日期是否在一年中的同一周。

看看: https://momentjs.com/docs/#/get-set/iso-weekday/

我认为做你需要的最好方法是告诉时刻你的一周从星期五开始。您可以简单地使用 updateLocale 方法自定义 week 对象的 dow(星期几)键,然后使用您的第一个代码片段。请参阅文档的 Customize 部分以获取有关区域设置自定义的更多信息。

这里是一个将自定义日期设置为一周的第一天然后使用您的代码检查给定日期是否在本周的实例:

moment.updateLocale('en', {
  week: {
    dow : 5, // Friday is the first day of the week.
  }
});

function checkWeek(dateLastVote){
  var weekNow = moment().week();
  var weekLastVote = moment(dateLastVote).week();
  if (weekNow == weekLastVote){
      //Prevent from voting again
      console.log(moment(dateLastVote).format('YYYY-MM-DD') + ' is in the current week')
  } else {
      //Let the user vote
      console.log(moment(dateLastVote).format('YYYY-MM-DD') + ' is NOT in the current week')
  }
}

checkWeek('2017-05-30'); // same week mon-sun, but previous week fri-thu
checkWeek('2017-06-01'); // same week mon-sun, but previous week fri-thu
checkWeek('2017-06-08'); // next week mon-sun, but current week fri-thu

// First day of the current week
console.log(moment().startOf('week').format('YYYY-MM-DD'));
// Last day of the current week
console.log(moment().endOf('week').format('YYYY-MM-DD'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

EDIT 一个改进的解决方案是使用时刻 isSame 传递 'week' 作为第二个参数。正如文档所述:

Check if a moment is the same as another moment.

If you want to limit the granularity to a unit other than milliseconds, pass it as the second parameter.

这是一个活生生的例子:

moment.updateLocale('en', {
  week: {
    dow : 5, // Friday is the first day of the week.
  }
});

function isSameWeek(dateLastVote){
  var now = moment();
  var lastVote = moment(dateLastVote);
  if (now.isSame(lastVote, 'week')){
      //Prevent from voting again
      console.log(moment(dateLastVote).format('YYYY-MM-DD') + ' is in the current week')
  } else {
      //Let the user vote
      console.log(moment(dateLastVote).format('YYYY-MM-DD') + ' is NOT in the current week')
  }
}

isSameWeek('2017-06-10'); // same week mon-sun, but next week fri-thu
isSameWeek('2017-06-03'); // previous week mon-sun, but current week fri-thu
isSameWeek('2017-06-06'); // current week both mon-sun and fri-thu

// First day of the current week
console.log(moment().startOf('week').format('YYYY-MM-DD'));
// Last day of the current week
console.log(moment().endOf('week').format('YYYY-MM-DD'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>