This is the first time I've written a code in Javascript, and I'm no better in other coding languages, but I managed to get the code to work (kinda).
I think this code could be way shorter but theres also another problem that I dont quite get. It would be really helpful for the future if someone could point out my mistakes and show me ways that are easier.
var main = function() {
//put the day of payment in here
const payday = 25;
//This code is needed for the following Calculation
const now = new Date();
currentDay = now.getDate();
const nextMonth = new Date(now.getFullYear(), now.getMonth() + 1, 1);
const diffDays = Math.ceil(Math.abs(nextMonth.getTime() - now.getTime()) / (1000 * 60 * 60 * 24));
now.setMonth(now.getMonth() + 1);
now.setDate(0);
const daysInCurrentMonth = now.getDate();
//Not necessary
//console.log('Days in current month: ' + daysInCurrentMonth);
//console.log('Days until next month: ' + diffDays);
//Calculation of days left until payment
//Here 25th is the day of Payment you can change that to your liking
const daysTilPayment = Math.ceil(diffDays - (daysInCurrentMonth - payday) - 1 );
//Folowing code is to determine if the payday is on a weekend
//Also it changes the paydate to 23th since thats how banks operate Here in Switzerland
//If your paydate will be changed to the closest workday (Friday) Then
//change the 2 on Rules for Saturdays to 1
//If that doesnt apply to you at all remove the 'Rules for Saturdays' + 'Rules for Sundays'
//Sunday = 0 and Saturday = 6
//to determine weekday of payment in current month
const d = new Date();
d.setDate(d.getDate() + daysTilPayment);
let paymentDay = d.getDay();
//to determine weekday of payment in next month
const thenextMonth = new Date (d.setMonth(d.getMonth() +4));
let paymentDay2 = d.getDay();
{
if (paymentDay >5)
//Rules for Saturdays
//To subtract 2
output = Math.ceil(daysTilPayment - 2);
//End of rules for Saturdays
else if (paymentDay <1)
//Rules for Sundays
//To subtract 2
output = Math.ceil(daysTilPayment - 2);
//End of rules for Sundays
else
//Rules for normal days
//does nothin
output = daysTilPayment
//End of rules for normal days
}
//this made the repetition above necessary
//it is to get rid of the countdown going negativ after payday
if (output < 0)
//output = Math.ceil(daysInCurrentMonth - currentDay + payday-2);
{
if (paymentDay2 >5)
//Rules for Saturdays
{//To put a 0 in front of single digit numbers and subtract 2
if (daysTilPayment+ daysInCurrentMonth - currentDay + payday -2 < 10)
output = ': 0'+Math.ceil(daysInCurrentMonth - currentDay + payday-2);
else
output = ': '+Math.ceil(daysInCurrentMonth - currentDay + payday-2);
}
//End of rules for Saturdays
else if (paymentDay2 <1)
//Rules for Sundays
{//To put a 0 in front of single digit numbers and subtract 2
if (daysTilPayment+ daysInCurrentMonth - currentDay + payday -2 < 10)
output = ': 0'+Math.ceil(daysInCurrentMonth - currentDay + payday-2);
else
output = ': '+Math.ceil(daysInCurrentMonth - currentDay + payday-2);
}
//End of rules for Sundays
else
//Rules for normal days
{//Just to put a 0 in front of single digit numbers
if (daysTilPayment+ daysInCurrentMonth - currentDay + payday < 10)
output = ': 0'+Math.ceil(daysInCurrentMonth - currentDay + payday);
else
output = ': '+Math.ceil(daysInCurrentMonth - currentDay + payday);
}
//End of rules for normal days
}
else
{
if (paymentDay >5)
//Rules for Saturdays
{//To put a 0 in front of single digit numbers and subtract 2
if (daysTilPayment -2 < 10)
output = ': 0'+Math.ceil(daysTilPayment - 2);
else
output = ': '+Math.ceil(daysTilPayment - 2);
}
//End of rules for Saturdays
else if (paymentDay <1)
//Rules for Sundays
{//To put a 0 in front of single digit numbers and subtract 2
if (daysTilPayment -2 < 10)
output = ': 0'+Math.ceil(daysTilPayment - 2);
else
output = ': '+Math.ceil(daysTilPayment - 2);
}
//End of rules for Sundays
else
//Rules for normal days
{//Just to put a 0 in front of single digit numbers
if (daysTilPayment < 10)
output = ': 0'+daysTilPayment;
else
output = ': '+daysTilPayment;
}
//End of rules for normal days
}
//for debug purpose
//console.log(output);
return output.toLocaleLowerCase('de-DE');
}
Since I want to Use this on a Widget Im returning to local lowercase but the output is sometimes a math result which cant be returned that way. To counter this I've put a : infront so that the output is a text and not a math result. It fixed the issue but now the widget always outputs :nn instead of just nn . Its no big of a deal I just want to now how it would have been done correctly.
Im sorry if i've talked nonsense. I really have no Idea of what im doing.
The OP code is convoluted and difficult to read. As far as I can see, it's supposed to calculate the number of days until pay day, which should be on the 25th of the month, or if that falls on a Sat or Sun, to be the previous Friday.
There are too many issues in the OP code to address each one. See Get difference between 2 dates in JavaScript? and the linked duplicate for some of the issues. Here's a simple solution based on the above rules.