exclude holidays and weekends when adding dates in javascript [duplicate] - javascript

This question already has answers here:
Set date to 7 working days from today (excluding weekends and public holidays)
(3 answers)
Closed 4 years ago.
I would like to add 10 days to a date excluding holidays and weekends. Here is the code i am using but i am unable to figure out how to take out the holidays and weekends. Thanks for any help!!
var someDate = item.INITIAL_REQUEST;
var numberOfDaysToAdd = 10;
someDate.setDate(someDate.getDate() + numberOfDaysToAdd);
item.FINAL_REQUEST = someDate;

There isn't a built-in command to do this.
Basically you have to add one date at a time, and test each one for whether it's a holiday or weekend. Weekends are easy:
function amIAWeekend(someDate){
return someDate.getDay() == 6 || someDate.getDay() == 0;
}
Testing for a holiday is much harder. The simplest thing to do might be to have a list of holidays in some sort of JSON format, and then for each date run through the list to see if it matches.
Hope this helps get you started!

Related

Getting the date of the nth day after a given date [duplicate]

This question already has answers here:
Incrementing a date in JavaScript
(19 answers)
How to add days to Date?
(56 answers)
Closed 2 years ago.
I am looking to find the date for, say, the 100th day after any given day. Is there a way to do that via javascript?
I was hoping it could be as simple as below, but it isn't quite that simple.
var givenDay = new Date(01/01/2020);
var hundredthDay = new Date(givenDay + 100);
console.log(hundredthDay)
You can try using .setDate() and .getDate() combination.
The setDate() method sets the day of the Date object relative to the beginning of the currently set month.
The getDate() method returns the day of the month for the specified date according to local time.
Adding the required days to .getDate() as the following:
const givenDay = new Date('01/01/2020');
console.log(givenDay);
const result = new Date(givenDay.setDate(givenDay.getDate() + 1 + 100));
console.log(result);
I hope this helps!

Incorrect date in moment.js getters [duplicate]

This question already has an answer here:
How to get the integer value of month from moment.js
(1 answer)
Closed 3 years ago.
EDIT: NB: As pointed out in the accepted answer from Zohaib Ijaz month gives the 0-11 value for the month but the second problem is day. day gives a numeric representation of day of the week ie 0 = sunday, 1 = tuesday. If you want to get the 1-31 you need date.
OK I think this must be a super dumb question but I just cannot see it. Today is 11th November 2019
var day = moment().get('day');
var month = moment().get('month');
var year = moment().get('year');
var dateSet = month+"/"+day+"/"+year;
console.log(dateSet);
and I get back: 10/01/2019!
I tried the functional getter of var day = moment().day(); etc. and same again.
I am trying to get today's date and the date in three months times for a datepicker. Greatful for an explanation of this and extremely grateful for a pointer as how to do the +3 months which I tried moment().plus(3,"months") which did not work.
I am extremely tired but I am pretty sure at one stage this was giving the right date. What could possibly have changed?
moment().month() or moment().get('month') will return month from 0 as January to 11 as December. So if you want to create date in MM/DD/YYYY format, user moment().format(format_string). Or add 1 in month while creating your date string. I would suggest to use format and go through moment docs first so you have better idea what it provides out of the box.
See docs
https://momentjs.com/docs/#/get-set/get/
moment().format('MM/DD/YYYY')

calculate whether weekend weekday or holiday [duplicate]

This question already has answers here:
Determine if a date is a Saturday or a Sunday using JavaScript
(6 answers)
Checking if date belongs to array of dates
(3 answers)
How to determine if date is weekend in JavaScript
(11 answers)
Moment JS Excluding Holidays
(2 answers)
Closed 3 years ago.
I have a start date and end date from my request need to find whether the given day is weekday or weekend or holiday(from list of holidays in db)
I tried using moment.js
moment.js is just a library to manage dates, what you need is an algorithm to find if one of the dates is weekend or weekday.
So what you can do is get the day number from the date (0-6) where 0 is sunday.
Let's say
var date = moment("2015-07-02");
var dow = date.day();
And about holidays or not or not, that's simple also, you need to get an array with all the dates in the same format that you are using with moment.js and iterate through the array to do it something like:
var holidays = ["2019-07-02", "2019-07-04"]
let isHoliday = holidays.find(x => x === "2015-07-02")
You need to do some work after all you need is here.
To determine if a given date is a weekend is actually pretty simple and already has an answer here.
To determine if a given date is holiday is bit more complex, you can not achieve it with native JavaScript, you will need the help of external package.
I recommend you to use moment-holiday, it has a simplest API:
moment('2017-12-25').isHoliday();
//Christmas Day
moment('2005-03-15').isHoliday();
//false
moment('2009-10-31').isHoliday('Halloween');
//true
moment('2017-12-31').isHoliday();
//New Year's Eve
moment('2017-12-31').isHoliday(null, true);
//false
Or you can use date-holidays.

Javascript Determine 10 days before specific date [duplicate]

This question already has answers here:
How to add days to Date?
(56 answers)
Closed 4 years ago.
So I am currently fetching some records from my database, and one of the records is a 'last edited' which is a date. However, I just want to display this 10 days before it has been exactly one year since the specific date. How would I approach this? I know how to deal with arrays and the fetching itself, but I'm not sure how I would deal with the date?
This is what I've done so far:
var startDate = fetchedDate;
var validDate = new Date(startDate);
var fullYear = validDate.getFullYear();
validDate.setFullYear(fullYear + 1);
And then I need to create a new date to compare it, I suppose? But how? I also want to know how many days it is until it has been one year.
If you wanna make it with pure javascript you can
var startDate = fetchedDate;
var validDate = new Date(startDate);
validDate.setYear(validDate.getFullYear() - 1);
validDate.setDate(validDate.getDate() - 10);
But I also will recommend you to use moment.js, is a very good javascript library to handle dates, otherwise sometimes is like trying to reinvent the wheel.

AngularJS how to add days / months / years to a date [duplicate]

This question already has answers here:
How to add number of days to today's date? [duplicate]
(16 answers)
Closed 8 years ago.
We are using AngularJS to set filters.
Basically we have a start date and end date and frequency. When the frequency is set to week, we simply want to add 1 week to the start date, when the frequency is set to daily, we want to add 1 day to the start date.
Basically something like :-
var date = new date();
date.addDays(2);
date.addMonths(2);
date.addYears(2);
I would consider using moment.js for all of your JS date related needs then you can do:
var date = moment();
date.add(2, 'days');
date.add(2, 'months');
date.add(2, 'years');
// or all of the above with:
date.add({years: 2, months: 2, days: 2});
And if you need a regular JS date object at the end, check out this post

Categories