Moment.js - Check if there are two points from the same week, but the weeks start on Friday and end on Thursday

I create a Discord bot with node.js and discord.js, and there is a feature that allows users to vote for the team, but I would like them to vote only once a week.

The problem is that on this Difference, the weeks start on Friday and end on Thursday , so I can't just write:

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

So I wrote code that seems to work, but I would like your opinion about it to look very sloppy, and I'm not sure that I’ve taken all the possibilities into account (I don’t know if I need to use my month variables, eg):

 module.exports = { isSameWeek: function (dateLastVote) { // moments for today 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 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; } } 

};

As I said, this seems like a trick, but I would like someone else to think about it to make sure that there is no easier way or, if not, if I have not forgotten anything.

Thank you for reading:)

+5
source share
3 answers

I don’t know how our approaches compare with each other in the field of productivity, but I still want to show my approach to the problem:

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

What the solution does is to calculate the start of the week of each of the dates with respect to the offset (for values> = -7 and <= 0) and the return if both have the same start of the week. Same week start = same week.

All you have to do is call a function that passes two date objects (or moment objects) and an offset between -7 and 0, depending on how the week is shifted relative to the "normal" week.

+4
source

I think the best way to do what you need is to say that your week starts on Friday. You can simply use the updateLocale method to configure the dow (day of the week) key of the week object, and then use your first piece of code. See the Customization section in the documents for more information on locale configuration.

Here is a live example of setting a custom day as the first day of the week, and then using the code to check if the given day is in the current week:

 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 An improved solution is to use the isSame moment, passing 'week' as the second parameter. As stated in the docs:

Check if the moment coincides with another moment.

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

Here's a living sample:

 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> 
+1
source

According to the Moment docs, you can set the start of ISO within a week:

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

then you can use the same functionality to check if the days are in the same week of the year.

Take a look: https://momentjs.com/docs/#/get-set/iso-weekday/

0
source

Source: https://habr.com/ru/post/1268472/


All Articles