How to display all weeks from two dates (not the number) - javascript

I want to show in a all weeks between two dates and with removing Saturday and Sunday from weeks.
so is there a function to get a list of days or weeks between two dates?
i want to show some thing like That :
when the user click the there is as option:
Week 1 : From 15 to 19 October
Week 2 : From 22 to 26 October

Related

How to get monday of the week which overlaps 2 months using moment js?

I need to create few records on Mondays using an api.
And the rule to get the Monday is that it should be either first Monday of a given month or last Monday of the given month.
For example:
If the input date range is 1 Sep 2020 - 30 Sep 2020, I need to pull 28 Sep 2020.
If the input date range is 1 Oct 2020 - 31 Oct 2020, I need to pull 26 Oct 2020.
If the input date range is 1 Nov 2020 - 30 Nov 2020, I need to pull 30 Nov 2020.
If the input date range is 1 Nov 2020 - 31 Dec 2020, I need to pull 28 Dec 2020.
What I did so far is I pulled all the Mondays of given month and stored them in an array. I tried pulling 1st or last Monday of the month, but this works for few months and not for few others.
Hence, I was thinking if I get to know the week of a given day(Monday), which overlaps 2 months, then I can pull that Monday.
So my question is how to get Monday of the week which overlaps 2 months? I am using moment js.
Maybe too late, but it could be helpful for future viewers.
The function takes start and end, but we only use end, so you can get rid of start as all the operations will be on end date in order to get the last monday. To achieve our goal we need to do two simple steps:
Get the end of the month with the end date.
Get the monday of the previous "calculated" date.
For the first step, we use End of Time in order to get the end of the month (the last day).
Mutates the original moment by setting it to the end of a unit of time.
Then we can get the Monday of the week the date is, and we can use Date of Week with the date of previous step.
Gets or sets the day of the week. [...] As of 2.1.0, a day name is also supported. This is parsed in the moment's current locale.
And taking your examples, we can test our function:
function lastMonday(start, end) {
console.log('Start is ', moment(start).format('LL'));
console.log('End is ', moment(end).format('LL'));
return moment(end).endOf('month').day('Monday');
}
console.log('Last monday is', lastMonday('2020-09-01', '2020-09-30').format('LL'));
console.log('Last monday is', lastMonday('2020-10-01', '2020-10-31').format('LL'));
console.log('Last monday is', lastMonday('2020-11-01', '2020-11-30').format('LL'));
console.log('Last monday is', lastMonday('2020-11-01', '2020-12-31').format('LL'));
<script src="https://momentjs.com/downloads/moment.js"></script>

Javascript date : go back to the last week number of year_n when on week number 1 of year_n+1

Initially, I got the number of week this week of month of 2020 which is week 43 (Oct 19 2020 - Oct 25 2020).
I also have two buttons which serve as next and previous.
When I click next button, the date changes to (Oct 26 2020, Nov 1 2020) as well as the week number to 44.
The same way goes to previous button. All is working almost fine, However, when I reach year 2021, the week number becomes 1 of course. and when I click previous again, it goes to the last week number of year 2021 instead of 2020.
My code for incrementing and decrementing weekly is this:
date.setDate(date.getDate()+7) //increment
date.setDate(date.getDate()-7) //decrement
I know the reason why it does not go back to the last week of 2020 because I only deducted 7 days.
However, I can't figure out how to go back to the last week of 2020 when I click previous.
Any idea?
UPDATE: Just now, I noticed, when I'm on the last week of 2020 and click next, Instead of going to '2021', It jumps to '2022'.. damn, I really hate working with dates on javascript.
I concluded. using native javascript date is such a hassle,
instead, I used momentjs.
all of the long code simplified by using
moment(date).add/subtract(no_of_days, 'd');
It automatically do the thing for you. no need to get the number of week and the year.

Javascript month is wrong [duplicate]

This question already has answers here:
`date.setMonth` causes the month to be set too high if `date` is at the end of the month
(8 answers)
Closed 5 years ago.
Please someone tell me why the month gets increased by 1 if its value is higher than 9. The code is below
var dd = new Date();
dd.setFullYear(2017);
dd.setMonth(7);
console.log("Month(Expecting 7, and received 7) = " + dd.getMonth());
dd.setMonth(10);
console.log("Month(Expecting 10, and received 11) = " + dd.getMonth());
Fiddler code at here - https://jsfiddle.net/vzmtp3ua/
Because 31 days do not exist in every month so the extra days are added to the next month.
Set Date as Oct 31, add a month so it would be November 31st since there are 30 days the date is moved to December 1st.
In some months there are less days then 31 , So extra days are added to the following month
It has to do with the number of days in that month making the date shift to the first day in the next month.
The documentation mentions this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setMonth
The current day of month will have an impact on the behaviour of this method. Conceptually it will add the number of days given by the current day of the month to the 1st day of the new month specified as the parameter, to return the new date. For example, if the current value is 31st August 2016, calling setMonth with a value of 1 will return 2nd March 2016. This is because in 2016 February had 29 days.
Not all the months have same number of days (31) so next one will be moved to next month
When you create the Date object on 31th of October and then set the month to November (= 10!), which does not have 31 days, it will switch to the 1st of next month (December 1st).
It's dependent on current date. You initialize the date to "now" (new Date()), and today is Oct 31st.
If the day on your Date object is 31st and you call setMonth(), extra days will get carried on to the next month for months shorter than 31 days. If you try to setMonth(1) it will mean Feb 31st, so you get Mar 3rd.
To avoid the problem you can pass second argument the setMonth, which is the day to be set, e.g. dd.setMonth(10,30) (Nov 30).

How to display the complete week in a table for a month

I have a requirement where I need to display the complete week (4-5 weeks) for a month. i.e. if a month begins with Friday(1st of Jan) it should still display in the same week. But in my case, it is not happening like that. As 1st of Jan starts from Friday, it is showing as a separate week.
As you can see in both the fiddles, Dec 31 is Thursday and Jan 1st is Friday. But since Jan 1st is a separate month, it is showing as a different week from Friday. But I don't want this to happen. I want all my week to display in a single table.
What changes should I make in my logic to make it correct? Where have I gone wrong? what logic should I add aside my while loop for it to display the entire week? Please help me.
Your logic is excluding the rest of the days of a week because you've only included items that belong to the month you've prescribed.
while (c.get(Calendar.MONTH) == month) {
....
}
You should add some logic within to check if the last 'week' has 7 days in it and if not then add the missing items.

Customized Financial Year - Moment Js

Is there a way to find out years and quarters based on a date range.
We are considering November as Start date and October as End date of financial year.
Ex: Nov prev Year to Oct next Year is considered as one financial year.
November 1, 2014 - October 31, 2015 - > It is considered as one financial year
Any record past Oct 31 will go to next financial year.
We also need to find Quarters as below
Quarter 1 = November, December & January
Quarter 2 = February, March, April
Quarter 3 = May, June, July
Quarter 4 = August, September, October
moment('2014-12-01').utc().quarter()-> it returns 4 considering Jan as Start date of financial year.
But it is considered as 1st quarter.
-- Code --
function getQuarter() {
var obj = {};
// Set Quarters form nov
obj.quarter1 = {start:Moment().month(10).startOf('month'),end:Moment().month(0).endOf('month').add(1,'years')};
obj.quarter2 = {start:Moment().month(1).startOf('month').add(1,'years'),end:Moment().month(3).endOf('month').add(1,'years')};
obj.quarter3 = {start:Moment().month(4).startOf('month').add(1,'years'),end:Moment().month(6).endOf('month').add(1,'years')};
obj.quarter4 = {start:Moment().month(7).startOf('month').add(1,'years'),end:Moment().month(9).endOf('month').add(1,'years')};
return obj;
}
have used this to set quarters starting from November.
Is there any alternative approach ?
I cannot think of any built in functions or libraries that allow a year to span across 2 years. You could easily write a FinacialYear object that does this though, and use it as a wrapper around the Date object. You would need a constructor that takes in a start year or end year, and bases all the calculations on that.
then just overload the toString() method to return "2014-2015", and a getQuarter(date) which uses a if/else to return 1...4.

Categories

Resources