Customized Financial Year - Moment Js - javascript

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.

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 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).

Get days of the week based on week number

Is there a way to get the Sunday thru Saturday dates of the week based on week number?
For example the current week number is 32, so I would want an array (or some return type) as such:
["Sunday, July 3rd, 2016", "Monday, July 4th, 2016".... etc ]
The documentation here states that use can use the syntax below, however this seems to simply gives me the current day (unless I'm reading the output wrong).
moment().weeks(32)
Output: Fri Aug 05 2016 11:19:39 GMT-0400
Would suggest to not go for additional libraries. Once you get first day for given week number using moment().weeks(32), write a simple method to return next seven days using moment().add() method.

date object returns random date for invalid months/day

This code:
var dt = new Date('82/66/2005');
...gives a value for dt as Wed Nov 25 1992 00:00:00 GMT+0530 (IST) on Firefox.
How?
Fiddle - http://jsfiddle.net/4jyT3/1/
That date is not in a format that is specified to be supported by the Date constructor. I'm not surprised to find that most engines call it invalid, but apparently Firefox is attempting to make a date out of that. Even though it's not specified, most engines support month/day/year and all, last I checked, support year/month/day. Firefox appears to be applying year/month/day to the input, where most browsers see the invalid values and go with "invalid."
So Firefox is seeing 82 for year (and assuming 1982), 66 for month, and 2005 for day. JavaScript is usually very forgiving about out-of-bound values on dates, moving to the next month and such as necessary. And that's what's happening here: If you take January 1st 1982 and add a further 65 months and 2004 days, you end up on Nov 25 1992.
This code will reliably give you the date you mentioned:
var dt = new Date(1982, 65, 2005);
(65 instead of 66 because in the number form, months start with 0, but in string form, they start with 1.)
I can't re-produce this in Chrome, but I'll explain how FireFox arrives here.
new Date('82-66-2005');
Invalid date, let's guess what you wanted based on what we know about dates.
hyphen - format is usually yyyy-mm-dd
yy is short for 19yy
there are 12 months, so month 13 is Januaray of next year
similarly for days
So using this knowledge lets estimate it, assuming 365.25 days per year and 30.5 days per month.
// input
var year = 1982, // 19yy
bigMonth = 66,
bigDay = 2005;
// adjust day for years
var day = bigDay % 365.25;
year = year + (bigDay - day) / 365.25;
// adjust day for months
bigDay = day;
day = bigDay % 30.5;
bigMonth = bigMonth + (bigDay - day) / 30.5;
// adjust month for years
var month = bigMonth % 12;
year = year + (bigMonth - month) / 12;
console.log(year, month, day); // 1992 11 26.25
So it would be about the Nov 26th, 1992, which is pretty close to how your browser calculated it (it didn't need to estimate).

Calculating Date in JavaScript

I am currently looking to calculate a custom date in JavaScript and my head is beginning to hurt thinking about it. I have a countdown clock that is to start every other Tuesday at 12pm. I have the countdown function working properly using the jQuery countdown plugin by Keith Wood but need assistance in calculating every other Tuesday of the month and having it reset on this day.
All help is greatly appreciated as always.
Thansk in advance
I've had to do something similar (not in JS but the algorithm is similar enough)
Now, before i start, to clarify i'm assuming this is something that happens fortnightly regardless of the length of the month, and not on the second and 4th Tuesday regardless of when it last happened, which is simpler to solve
Pick a date in the past that this event has occured on (or the date of the first occurrence) , we'll call this date base in the following code
var base=new Date('date of first occurrence');
var one_day=1000*60*60*24; //length of day in ms
// assume we care about if the countdown should start today
// this may be different if you are building a calendar etc.
var date_to_check=new Date();
var diff_in_days=math.floor(date_to_check-base)/one_day);
var days_since_last_reset= diff_in_days%14;
if(days_since_last_reset == 0){
//date_to_check is the same day in the fortnightly cycle as base
//i.e. today at some point is when you'll want to show the timer
//If you only want to show the timer between certain times,
//add another check here
}else{
//next reset in (14 - days_since_last_reset) days from date_to_check
}
Or the code-golf-esque version:
if( Math.floor((new Date()-new Date('date of first occurrence'))/1000/60/60/24)%14 == 0 )
//reset/start timer
Please find attached link for Date Library to get the custom calculation date and time functions.
To use it client side, download index.js and assertHelper.js and include that in your HTML.
<script src="assertHelper.js"></script>
<script type="text/javascript" src="index.js"></script>
$( document ).ready(function() {
DateLibrary.getDayOfWeek(new Date("2015-06-15"),{operationType:"Day_of_Week"}); // Output : Monday
}
You can use different functions as given in examples to get custom dates.
To get First Day of quarter From Given Date
DateLibrary.getRelativeDate(new Date("2015-06-15"),
{operationType:"First_Date",granularityType:"Quarters"}) // Output : Wed Apr 01 2015 00:00:00
If first day of week is Sunday, what date will be on Wednesday, if
given date is 15th June 2015
DateLibrary.getRelativeDate(iDate,
{operationType: "Date_of_Weekday_in_Week",
startDayOfWeek:"Sunday",returnDayOfWeek:"Wednesday"}) // Output : Wed Jun 17 2015 00:00:00
If first day of week is Friday, what date will be on Tuesday of 3rd
Week of 2nd month of 3rd quarter of year containing 15th June 2015 as
one of the date.
DateLibrary.getRelativeDate(new Date("2015-06-15"),
{operationType: "Date_of_Weekday_in_Year_for_Given_Quarter_and_Month_and_Week",
startDayOfWeek:"Friday",returnDayOfWeek:"Tuesday", QuarterOfYear:3, MonthOfQuarter:2, WeekOfMonth:3}) // Output : 18th Aug 2015
If first day of week is Tuesday, what week number in year will be
follow in 15th June 2015 as one of the date.
DateLibrary.getWeekNumber(new Date("2015-06-15"),
{operationType:"Week_of_Year",
startDayOfWeek:"Tuesday"}) // Output : 24
There are Date Difference functions also available
DateLibrary.getDateDifference(new Date("2016-04-01"),new Date("2016-04-16"),
{granularityType: "days"}) //output 15
Function for Convert number to Timestr
DateLibrary.getNumberToTimeStr("345", {delimiter: ":"}) //output 00:03:45
It also supports Julian date conversion
DateLibrary.julianToDate("102536") //output Fri Jun 20 2003 00:00:00
There is a JavaScript implementation of RFC 2445 recurrence rules : http://code.google.com/p/google-caja/source/browse/trunk/src/com/google/caja/demos/calendar/rrule-cajita.js which requires some files in the same directory. See the unit test ( http://code.google.com/p/google-caja/source/browse/trunk/tests/com/google/caja/demos/calendar/rrule_test.js ) for examples of how it works.
Try using it to parse RRULE:FREQ=WEEKLY;BYDAY=TU;INTERVAL=2 which means every second (because of the interval) week (because of the frequency) on Tuesday (because of the byday).
Have a look at the date.js library. It has several date parsing helpers including Date.today().next().tuesday() (among others).

Categories

Resources