I'm trying to display an array of possible delivery dates using AngularJS and MomentJS.
The issue is that it needs to meet certain conditions: Delivery dates are only Monday, Wednesday and Fridays.
Also, when the page loads, it recognizes the current date and it will only display the next available date that is minimum 24h away (e.g., if I load the page on a Sunday at 1pm, the first available date will be Wednesday, as Monday doesn't meet the 24h margin).
So far I could only think if dealing with the issue doing conditionals for every day of the week, but I'm pretty sure there has to be a neater way of dealing with it.
Here's what I did so far:
$scope.today = moment();
$scope.$watch('today', function () {
if ($scope.today = moment().day('Sunday')){
$scope.nextdateone = moment().add(3, 'd');
$scope.nextdatetwo = moment().add(5, 'd');
$scope.nextdatethree = moment().add(8, 'd');
$scope.nextdatefour = moment().add(10, 'd');
}
else if ($scope.today = moment().day('Monday')){
$scope.nextdateone = moment().add(2, 'd');
$scope.nextdatetwo = moment().add(4, 'd');
$scope.nextdatethree = moment().add(7, 'd');
$scope.nextdatefour = moment().add(9, 'd');
}
else if ...
});
This was the logic I came up with, but it doesn't really work as of now...
Any tips?
The delivery dates "Monday, Wednesday and Fridays", which (according to http://momentjs.com/docs/#/get-set/day/) you can represent as 1, 3 and 5.
So I would create a array with those dates, and then given the current day I would iterate that array of delivery dates to find the most suitable one... something like this:
const deliveryDates = [1, 3, 5];
const getDeliveryDate = (today) => {
let deliveryIndex = -1;
deliveryDates.some((date, index) => {
// If today is a delivery date, then schedule for the next delivery
if (today === date) {
deliveryIndex = index + 1;
return true;
}
// If today is before the current delivery date, store it
if (today < date) {
deliveryIndex = index;
return true;
}
});
// If delivery date is out of bounds, return the first delivery date
return deliveryIndex === deliveryDates.length || deliveryIndex === -1 ? 0 : deliveryIndex;
};
const getNextDelivery = (today) => {
return deliveryDates[getDeliveryDate(today)];
};
console.log(moment().day(getNextDelivery(moment().day())));
You can check a working example here:
https://jsbin.com/jawexafiji/edit?js,console
Related
I have a date range that looks like this
let start = moment(this.absence.FromDate);
let end = moment(this.absence.ToDate);
The user can decide to deactivate specific week days during that date range, so I have booleans
monday = true;
tuesday = false;
...
I want to create a function that allows me to put all mondays during my date range in an array.
I've looked around on stack but I can only find help for people who need all the monday from a month for example.
You can get next Monday using .day(1) and then loop until your date isBefore your end date adding 7 days for each iteration using add
Here a live sample:
//let start = moment(this.absence.FromDate);
//let end = moment(this.absence.ToDate);
// Test values
let start = moment();
let end = moment().add(45 , 'd');
var arr = [];
// Get "next" monday
let tmp = start.clone().day(1);
if( tmp.isAfter(start, 'd') ){
arr.push(tmp.format('YYYY-MM-DD'));
}
while( tmp.isBefore(end) ){
tmp.add(7, 'days');
arr.push(tmp.format('YYYY-MM-DD'));
}
console.log(arr);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
I am creating a Discord bot with node.js and discord.js, and there's a feature that allows users to vote thanks to a command, but I'd like them to vote only once a week.
The issue is that, on this Discord, weeks start on Friday and end on Thursday, therefore I can't simply write :
var weekNow = moment().week();
var weekLastVote = moment(dateLastVote).week();
if (weekNow == weekLastVote){
//Prevent from voting again
} else {
//Let the user vote
}
Therefore, I have written some code that seems to work, but I'd like your opinion on it as it seems very sloppy and I'm not sure if I have taken into account all of the possibilities (I don't know if I need to use my month variables for example):
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;
}
}
};
As I said, this seems do to the trick but I would like someone else's opinion on it to be sure there isn't a simpler way or, if there isn't, if I haven't forgotten anything.
Thank you for reading :)
I do not know how our approaches compare to each other in matter of performance, but I still wanna show my approach on 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 calculating the start of the week of each of the given dates in respect to the offset (for values >= -7 and <= 0) and returning whether both have the same start of the week. Same start of the week = same week.
All you have to do is call the function passing two date objects (or moment objects) and an offset between -7 and 0, depending on how the week is shifted in relation to a "regular" week.
I think that the best way to do want you need is to tell moment that your week starts on Friday. You can simply use updateLocale method customizing dow (day of week) key of the week object and then use your first code snippet. See Customize section of the docs to get more info about locale customization.
Here a live example of setting a custom day as first day of the week and then using your code to check if a 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 moment isSame passing 'week' as second parameter. As the docs states:
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.
Here a live 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>
According to the Moment docs you can set the ISO start of the 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/
How to check if time is the same for Moment objects with different dates?
For example I have object like
const endDate = moment().add(30, 'days').endOf('day');
and I want to check if some moment object is endOf day.
private isEndOfDay(dateTime: string) {
const m = moment().endOf('day');
return m.isSame(dateTime, 'minute');
}
const receivedDateFormat: string = 'YYYY-MM-DD hh:mm:ss';
this.isEndOfDay(this.endDate.format(this.receivedDateFormat))
But for this case, when I pass "minute" parameter, it will check minute, hour, day, month and year... which isn't what I want to check.
The part of the documentation that explains that behaviour is
When including a second parameter, it will match all units equal or larger. Passing in month will check month and year. Passing in day will check day, month, and year.
So, if you just want to compare the minutes, you'll need to do something like
endDate.minute() === startDate.minute()
To compare the time only, format() the dates
endDate.format('HH:mm:ss') === startDate.format('HH:mm:ss')
To compare only time part you can set a given date (year, month and day) to your input.
Please note that passing 'minute' to isSame will ignore seconds.
Here a live sample:
function isEndOfDay(dateTime) {
let m = moment().endOf('day');
let m2 = moment(dateTime);
m2.set({
y: m.year(),
M: m.month(),
D: m.date()
});
return m.isSame(m2, 'minute');
}
var endDate = moment().add(30, 'days').endOf('day');
const receivedDateFormat = 'YYYY-MM-DD hh:mm:ss';
var ret = isEndOfDay(endDate.format(this.receivedDateFormat))
console.log(ret);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
Another way to to is checking only units that matter for you:
function isEndOfDay(dateTime) {
let m = moment().endOf('day');
let m2 = moment(dateTime);
if( m.hours() === m2.hours() &&
m.minutes() === m2.minutes() &&
m.seconds() === m2.seconds() ){
return true;
}
return false;
}
var endDate = moment().add(30, 'days').endOf('day');
const receivedDateFormat = 'YYYY-MM-DD hh:mm:ss';
var ret = isEndOfDay(endDate.format(this.receivedDateFormat))
console.log(ret);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
See Get + Set section of the docs to see how to get and set units of moment objects.
I want to get the date of the next Monday or Thursday (or today if it is Mon or Thurs). As Moment.js works within the bounds of a Sunday - Saturday, I'm having to work out the current day and calculate the next Monday or Thursday based on that:
if (moment().format("dddd")=="Sunday") { var nextDay = moment().day(1); }
if (moment().format("dddd")=="Monday") { var nextDay = moment().day(1); }
if (moment().format("dddd")=="Tuesday") { var nextDay = moment().day(4); }
if (moment().format("dddd")=="Wednesday") { var nextDay = moment().day(4); }
if (moment().format("dddd")=="Thursday") { var nextDay = moment().day(4); }
if (moment().format("dddd")=="Friday") { var nextDay = moment(.day(8); }
if (moment().format("dddd")=="Saturday") { var nextDay = moment().day(8); }
This works, but surely there's a better way!
The trick here isn't in using Moment to go to a particular day from today. It's generalizing it, so you can use it with any day, regardless of where you are in the week.
First you need to know where you are in the week: moment().day(), or the slightly more predictable (in spite of locale) moment().isoWeekday(). Critically, these methods return an integer, which makes it easy to use comparison operators to determine where you are in the week, relative to your targets.
Use that to know if today's day is smaller or bigger than the day you want. If it's smaller/equal, you can simply use this week's instance of Monday or Thursday...
const dayINeed = 4; // for Thursday
const today = moment().isoWeekday();
if (today <= dayINeed) {
return moment().isoWeekday(dayINeed);
}
But, if today is bigger than the day we want, you want to use the same day of next week: "the monday of next week", regardless of where you are in the current week. In a nutshell, you want to first go into next week, using moment().add(1, 'weeks'). Once you're in next week, you can select the day you want, using moment().day(1).
Together:
const dayINeed = 4; // for Thursday
const today = moment().isoWeekday();
// if we haven't yet passed the day of the week that I need:
if (today <= dayINeed) {
// then just give me this week's instance of that day
return moment().isoWeekday(dayINeed);
} else {
// otherwise, give me *next week's* instance of that same day
return moment().add(1, 'weeks').isoWeekday(dayINeed);
}
See also https://stackoverflow.com/a/27305748/800457
EDIT: other commenters have pointed out that the OP wanted something more specific than this: the next of an array of values ("the next Monday or Thursday"), not merely the next instance of some arbitrary day. OK, cool.
The general solution is the beginning of the total solution. Instead of comparing for a single day, we're comparing to an array of days: [1,4]:
const daysINeed = [1,4]; // Monday, Thursday
// we will assume the days are in order for this demo, but inputs should be sanitized and sorted
function isThisInFuture(targetDayNum) {
// param: positive integer for weekday
// returns: matching moment or false
const todayNum = moment().isoWeekday();
if (todayNum <= targetDayNum) {
return moment().isoWeekday(targetDayNum);
}
return false;
}
function findNextInstanceInDaysArray(daysArray) {
// iterate the array of days and find all possible matches
const tests = daysINeed.map(isThisInFuture);
// select the first matching day of this week, ignoring subsequent ones, by finding the first moment object
const thisWeek = tests.find((sample) => {return sample instanceof moment});
// but if there are none, we'll return the first valid day of next week (again, assuming the days are sorted)
return thisWeek || moment().add(1, 'weeks').isoWeekday(daysINeed[0]);;
}
findNextInstanceInDaysArray(daysINeed);
I'll note that some later posters provided a very lean solution that hard-codes an array of valid numeric values. If you always expect to search the same days, and don't need to generalize for other searches, that'll be the more computationally efficient solution, although not the easiest to read, and impossible to extend.
get the next monday using moment
moment().startOf('isoWeek').add(1, 'week');
moment().day() will give you a number referring to the day_of_week.
What's even better: moment().day(1 + 7) and moment().day(4 + 7) will give you next Monday, next Thursday respectively.
See more: http://momentjs.com/docs/#/get-set/day/
The following can be used to get any next weekday date from now (or any date)
var weekDayToFind = moment().day('Monday').weekday(); //change to searched day name
var searchDate = moment(); //now or change to any date
while (searchDate.weekday() !== weekDayToFind){
searchDate.add(1, 'day');
}
Most of these answers do not address the OP's question. Andrejs Kuzmins' is the best, but I would improve on it a little more so the algorithm accounts for locale.
var nextMoOrTh = moment().isoWeekday([1,4,4,4,8,8,8][moment().isoWeekday()-1]);
Here's a solution to find the next Monday, or today if it is Monday:
const dayOfWeek = moment().day('monday').hour(0).minute(0).second(0);
const endOfToday = moment().hour(23).minute(59).second(59);
if(dayOfWeek.isBefore(endOfToday)) {
dayOfWeek.add(1, 'weeks');
}
Next Monday or any other day
moment().startOf('isoWeek').add(1, 'week').day("monday");
IMHO more elegant way:
var setDays = [ 1, 1, 4, 4, 4, 8, 8 ],
nextDay = moment().day( setDays[moment().day()] );
Here's e.g. next Monday:
var chosenWeekday = 1 // Monday
var nextChosenWeekday = chosenWeekday < moment().weekday() ? moment().weekday(chosenWeekday + 7) : moment().weekday(chosenWeekday)
The idea is similar to the one of XML, but avoids the if / else statement by simply adding the missing days to the current day.
const desiredWeekday = 4; // Thursday
const currentWeekday = moment().isoWeekday();
const missingDays = ((desiredWeekday - currentWeekday) + 7) % 7;
const nextThursday = moment().add(missingDays, "days");
We only go "to the future" by ensuring that the days added are between 0 and 6.
Is there a simple/built in way of figuring out the current financial quarter?
ex:
Jan-Mar: 1st
Apr-Jul: 2nd
Jul-Sept: 3rd
Oct-Dec: 4th
This is now supported in moment:
moment('2014-12-01').utc().quarter() //outputs 4
moment().quarter(); //outputs current quarter ie. 2
Documentation
Using version 2.14.1+ you can do something like the following:
moment().quarter() returns the current quarter number: 1, 2, 3, 4.
moment().quarter(moment().quarter()).startOf('quarter');
Would return the current quarter with the date set to the quarter starting date.
moment().quarter(moment().quarter()).startOf('quarter');
Would return the current quarter with the date set to quarter ending date.
You could also define a function that takes the corresponding quarter number as argument (1,2,3,4), and returns an object containing the start and end date of the quarter.
function getQuarterRange(quarter) {
const start = moment().quarter(quarter).startOf('quarter');
const end = moment().quarter(quarter).endOf('quarter');
return {start, end};
}
Use this simple code to get all quarter based on january and april
Demo
Code :
// startMonth should be january or april
function setQuarter(startMonth) {
var obj = {};
if(startMonth=='january'){
obj.quarter1 = {start:moment().month(0).startOf('month'),end:moment().month(2).endOf('month')}
obj.quarter2 = {start:moment().month(3).startOf('month'),end:moment().month(5).endOf('month')}
obj.quarter3 = {start:moment().month(6).startOf('month'),end:moment().month(8).endOf('month')}
obj.quarter4 = {start:moment().month(9).startOf('month'),end:moment().month(11).endOf('month')}
console.log(obj);
return obj;
}
else if(startMonth=='april'){
obj.quarter1 = {start:moment().month(3).startOf('month'),end:moment().month(5).endOf('month')}
obj.quarter2 = {start:moment().month(6).startOf('month'),end:moment().month(8).endOf('month')}
obj.quarter3 = {start:moment().month(9).startOf('month'),end:moment().month(11).endOf('month')}
obj.quarter4 = {start:moment().month(0).startOf('month').add('years',1),end:moment().month(2).endOf('month').add('years',1)}
console.log(obj);
return obj;
}
}
setQuarter('april');
Fiddle
START DATE
moment().quarter(moment().quarter()).startOf('quarter');
Would return the current quarter with the date set to the quarter starting date.
moment("2019", "YYYY").quarter(4).startOf('quarter');
Would return the starting date of the 4th quarter of the year "2019".
moment().startOf('quarter');
Would return the starting date of the current quarter of current year.
END DATE
moment().quarter(moment().quarter()).endOf('quarter');
Would return the current quarter with the date set to quarter ending date.
moment("2019", "YYYY").quarter(4).endOf('quarter');
Would return the ending date of the 4th quarter of the year "2019".
moment().endOf('quarter');
Would return the ending date of the current quarter of current year.
I dont think any of these answers explain how to get the financial quarter. They explain how to get the calendar quarter.
I do not have a clean answer as thats what led me here. But the fiscal quarter is what is really wanted. And that is based on the start month of the fiscal year.
For example if my company's fiscal start month is February. Then at the time of writing this January 9th 2017 I'm actually in Q4 2016.
To accomplish this we need a way to get the quarter relative to a supplied integer of the start month.
There is nothing built in right now, but there is conversation to add formatting tokens for quarters. https://github.com/timrwood/moment/pull/540
In the meantime, you could use something like the following.
Math.floor(moment().month() / 3) + 1;
Or, if you want it on the moment prototype, do this.
moment.fn.quarter = function () {
return Math.floor(this.month() / 3) + 1;
}
The formula that seems to work for me is:
Math.ceil((moment().month() + 1) / 3);
moment().month() gives back the 0-11 month format so we have to add one
THE ACTUAL MONTH = (moment().month() + 1)
then we have to divide by 3 since there are 3 months in a quarter.
HOW MANY QUARTERS PASSED = (THE ACTUAL MONTH) / 3
and then we have to get the ceiling of that (round to the nearest quarter end)
CEILING(HOW MANY QUARTERS PASSED)
EDIT:
The Official formula (not commited yet) is:
~~((this.month()) / 3) + 1;
which means Math.floor((this.month()) / 3) + 1;
The simplist way to do this is
Math.floor(moment.month() / 3)
That will give you the zero based quarter index. ie 0, 1, 2, or 3.
Then, if you want the quarter's literal number, just add one.
Answer given by Nishchit Dhanani, is correct but has one issue in 'April' scenario.
Issue: If your financial year is April than, For first 3 months i.e. JAN, FEB & MAR
obj.quarter1.start date returns, 1-April-CurrentYear [incorrect Value]
obj.quarter4.end date retunrs, 31-March-NextYear [incorrect Value]
Correct values should be,
Start = 1-April-PreviuosYear
End = 31-March-CurrentYear
So, Taking consideration for first 3 month it can be written something like,
const obj = {};
/* 0-Jan, 1-Feb, 2-Mar */
if (moment().month() <= 2) {
obj.quarter1 = { start: moment().month(3).startOf('month').add('years', -1), end: moment().month(5).endOf('month').add('years', -1) };
obj.quarter2 = { start: moment().month(6).startOf('month').add('years', -1), end: moment().month(8).endOf('month').add('years', -1) };
obj.quarter3 = { start: moment().month(9).startOf('month').add('years', -1), end: moment().month(11).endOf('month').add('years', -1) };
obj.quarter4 = { start: moment().month(0).startOf('month'), end: moment().month(2).endOf('month') };
} else {
obj.quarter1 = { start: moment().month(3).startOf('month'), end: moment().month(5).endOf('month') };
obj.quarter2 = { start: moment().month(6).startOf('month'), end: moment().month(8).endOf('month') };
obj.quarter3 = { start: moment().month(9).startOf('month'), end: moment().month(11).endOf('month') };
obj.quarter4 = { start: moment().month(0).startOf('month').add('years', 1), end: moment().month(2).endOf('month').add('years', 1) };
}
console.log(obj);