Script to display date for coming bi-monthly Friday - javascript

Cut Off Date: the cut off date is set as the coming bimonthly Friday (eg cut off dates are Jun 14 and Jun 28. If the submission is done today(June26th), then cut off date is Jun 28. If the submission is done on Jun 29th, the cut off date would be Jul 12. Once it's July 13th the next cutoffdate would be July 26th. And so on, depending on when the user submits the form it will display the correct cutoffdate. I've looked around at a few other examples, but can't find one for what I want.
var currentDate = new Date(new Date().getTime())
document.getElementById('lastsubmission').value =
(currentDate.getDate()) + '/' + (currentDate.getMonth() + 1) + '/' +
currentDate.getFullYear() + '#' + currentDate.getHours() + ':' +
currentDate.getMinutes() + ':' + currentDate.getSeconds();

You seem to want to get the next Friday after a date that is either the second or fourth Fridays of a month. Something like:
Get the second Friday of the date's month
If it's before that, return the second Friday
If it's after then, get the fourth Friday of the month
If it's before then, return the fourth Friday
If it's after then, return the second Friday of next month
So you might need a function to get a particular Friday given a date, and some other code to do the rest of the logic, e.g.
// Given a Date, return the nth Friday of that month
function getNthFriday(date, n) {
// Get first Friday
let d = new Date(date);
d.setDate(1);
let day = d.getDay();
d.setDate(d.getDate() + 5 - (day > 5? -1 : day));
// Set to nth Friday
d.setDate(d.getDate() + (n-1)*7);
return d;
}
// Return the next Friday after date that is either the
// second or fourth Friday's of a month.
function getCutoffDate(date) {
// Get 2nd Friday
var friday = getNthFriday(date, 2);
// If before, return 2nd Friday
if (date < friday) {
return friday;
}
// Get 4th Friday
friday = getNthFriday(date, 4);
// If before, return 4th Friday
if (date < friday) {
return friday;
}
// Otherwise, return 2nd Friday of next month
friday = getNthFriday(new Date(date.getFullYear(), date.getMonth()+1, 1), 2);
return friday;
}
// Some tests
[
new Date(2019,4,30), // 30 May 2019 -> 14 Jun
new Date(2019,5, 1), // 2 Jun 2019 -> 14 Jun
new Date(2019,5,13), // 13 Jun 2019 -> 28 Jun
new Date(2019,5,23), // 23 Jun 2019 -> 14 Jun
new Date(2019,5,30) // 30 Jun 2019 -> 12 Jul
].forEach(d => {
console.log(getCutoffDate(d).toString());
});
A library might help with getting the nth Friday, and getting the first day of the next month.

Related

Getting Week Numbers of a Year where week number is starting from January 1st

I have the following code to get the week number when I rpovide a date
Date.prototype.getWeek = function () {
var d = new Date(Date.UTC(this.getFullYear(), this.getMonth(), this.getDate()));
d.setUTCDate(d.getUTCDate() - d.getUTCDay());
var yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
return Math.ceil((((d - yearStart) / 86400000) + 1) / 7);
}
when I pass the date 2018-01-01 the weekNumber is given as 53 as january 1st of 2018 lay on the last week of 2017. How can I get the week number by making the year start at january 1st instead of making the the start day of each week is a sunday ?
You can calculate the number of whole weeks since Jan 1 using UTC date values to avoid daylight saving issues. Starting from 1ms before 1 Jan and using Math.ceil means 1 Jan is the first day of week 1.
The following is a slight refactoring of your code:
// Get week number based on start of 1 Jan
function getWeekNumber(date = new Date()) {
const msPerWeek = 5.828e8;
// Use UTC values to remove timezone issues
let d = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()));
// Set epoch to 1 ms before 1 Jan
let e = new Date(Date.UTC(d.getUTCFullYear(), 0, 0, 23, 59, 59, 999));
// Return week number
return Math.ceil((d - e) / msPerWeek);
}
[new Date(2019,0,1), // Tue 1 Jan, week 1
new Date(2019,0,7), // Mon 7 Jan, week 1
new Date(2019,0,8), // Tue 8 Jan, week 2
new Date(2019,0,15), // Tue 15 Jan, week 3
new Date() // Today
].forEach( date => console.log(date.toDateString() + ': ' + getWeekNumber(date)));

Moment.js, Get next [weekday]

Trying to get second coming Thursday (e.g.) using moment.js. Not this Thursday. The next one. The date in 2 Thursdays.
I have tried
moment().add(1, 'week').day(4)
which just fetches Thursday of the next week (only works if current weekday is before Thursday)
Any ideas?
"which just fetches Thursday of the next week (only works if current weekday is before Thursday)"
It's happening because .add(1, 'week') just adds 7 days and gets you the next week date and you are fetching 4th day of that week.
Below code will work for your case perfectly.
if(moment().weekday() < 4)
moment().add(1, 'week').day(4);
else
moment().add(2, 'week').day(4);
Not sure about using moment.js, but in plain js next Thursday is given by:
currentDate + (11 - d.getDay() % 7)
For the following Thursday, just add 7 days. Presumably if the current day is Thursday, want the Thursday in two weeks so:
var d = new Date();
console.log(d.toString());
// Shift to next Thursday
d.setDate(d.getDate() + ((11 - d.getDay()) % 7 || 7) + 7)
console.log(d.toString())
Or encapsulated in a function with some tests:
function nextThursdayWeek(d) {
d = d || new Date();
d.setDate(d.getDate() + ((11 - d.getDay()) % 7 || 7) + 7);
return d;
}
// Test data
[new Date(2017,2,6), // Mon 6 Mar 2017
new Date(2017,2,7), // Tue 7 Mar 2017
new Date(2017,2,8), // Wed 8 Mar 2017
new Date(2017,2,9), // Thu 9 Mar 2017
new Date(2017,2,10), // Fri 10 Mar 2017
new Date(2017,2,11), // Sat 11 Mar 2017
new Date(2017,2,12), // Sun 12 Mar 2017
new Date() // Today
].forEach(function (date) {
console.log(date.toString() + ' -> ' + nextThursdayWeek(date).toString());
});

Are there any JavaScript methods that can replicate the Oracle's ADD_MONTH() functionality

1) Oracle's example of ADD_MONTHS(date, 1):
SELECT ADD_MONTHS('30-Nov-15', 3) FROM dual;
February, 29 2016 00:00:00
2) JavaScript:
var date= new Date("Mon Nov 30 2015 00:00:00");
date.setMonth(date.getMonth() + 3);
Tue Mar 01 2016 00:00:00
Are there any JavaScript methods that can replicate the Oracle's ADD_MONTH() functionality ?
If you want to implement the same logik as in Oracle function - i.e. for "shorter" month you do not overflow in the next month, I guess you will need to do it yourself:
Pseudocode:
myDay = date.getDate(); // save the date
date.setMonth(date.getMonth() + 3); // add months
myNewDay = date.getDate();
while (myDay != myNewDay & myNewDay <= 3) {
myNewDay = myNewDay -1 // go back one day
date.setDate(myNewDay); // restore the
}
So if you end with the same day of the month after adding months you are ready.
If you get a different day of month, it will be 1,2 or 3 (the difference in month length);
go back day by day until you reach the end of the month.
This is my knowledge of the Oracle algorithm. HTH.
date.getMonth()
returns the previous months date instead of this months date. So to add to the correct date just do
date.setMonth(date.getMonth() + 2);

Get start and end day of week considering year starting in the middle of week [duplicate]

This question already has answers here:
How to get first and last day of the current week in JavaScript
(32 answers)
Closed 7 years ago.
I was using a function to return me the start and end day of a given week number from the year. Here is the function:
function getWeekRange(week){
var d = new Date("Jan 01, " + $scope.today.getFullYear() + " 01:00:00");
var w = d.getTime() + 604800000 * (week);
var d1 = new Date(w);
var d2 = new Date(w + 518400000);
return {
startDate: d1,
endDate: d2,
};
}
I set the year dynamically, and let's see an example where the year is 2015. Considering the week number starting by 0, if I use getWeekRange(0) I will receive the following result:
{
startDate: Thu Jan 01 2015 01:00:00 GMT-0200 (BRST),
endDate: Wed Jan 07 2015 01:00:00 GMT-0200 (BRST)
}
The problem is that this code does not consider the year of 2015 starting on a Thursday. The correct result for getWeekRange(0) should be:
{
startDate: Thu Jan 01 2015 01:00:00 GMT-0200 (BRST),
endDate: Sat Jan 03 2015 01:00:00 GMT-0200 (BRST)
}
and the result for getWeekRange(1) should be:
{
startDate: Sun Jan 04 2015 01:00:00 GMT-0200 (BRST),
endDate: Sat Jan 10 2015 01:00:00 GMT-0200 (BRST)
}
Does anyone have a clue?
-- EDIT --
My question is different from this one, because I don't have a given day of the year, I have only a week number of the year (from 0 to 51), and my case considers that the first week of the year is only a part of a full week, as mentioned by likeitlikeit.
I could find a simple solution for my question, since only the first week of my year could cause me problems. Here is the code:
function getWeekRange(week){
var d = new Date("Jan 01, " + $scope.today.getFullYear() + " 01:00:00");
var firstWeekDays = 7 - d.getDay();
var d1, d2;
if(week > 0) {
var w = d.getTime() + 604800000 * (week-1) + 24*60*60*1000 * firstWeekDays;
d1 = new Date(w);
d2 = new Date(w + 518400000);
} else {
d1 = d;
d2 = new Date(d1.getTime() + 24*60*60*1000 * (6 - d1.getDay()));
}
return {
startDate: d1,
endDate: d2,
};
}
There are 2 main diferences from the initial code.
For the week of the year I simply get the initial day of the year, and then based on the day of the week it starts I can find the end day of the week
For the other weeks, I sum 7*week-1 days to initial day (this sum does not consider the number of days of the first week), and also add firstWeekDays which is the number of days of first week (because it is not always 7 as the other weeks).
If anyone has a better solution, I will be glad to listen.
This answer shows how to get the first day of a week using getDate to calculate the offset from the current day.
Using this method, you can use getDate to determine which day of the week the first day of the year falls on. You can then subtract the last day of the week from this value to know how many days to add to your d2 in order to compute the date for Saturday by adjusting your date from the first day of the year + the offset in days till the end of the week for Jan 1.

Get the first day name (Mon, Tues...) of the month

As the title says I'm trying to get the first day name that a given month starts on. So for example if Oct 2014 was given the code would return 3 because there are 3 empty blocks before Wed the 1st. Again if I provided Jan 2015, I should get 4. Here is my function:
function getDayMonthStartsOn(month, year) {
var day = new Date(year + "-" + month + "-01").getDay();
day = (day === 0) ? 7 : day;
return day;
}
My problem is that it seems to work for Jan 2015 but not Oct 2014.
Use getUTCDay() instead of getDay().

Categories

Resources