Javascript 中不同发薪日的倒计时

问题描述 投票:0回答:1

这是我第一次用 Javascript 编写代码,我在其他编码语言中也没有更好,但我设法让代码工作(有点)。

我认为这段代码可以更短,但还有另一个我不太明白的问题。 如果有人能指出我的错误并告诉我更简单的方法,这对未来真的很有帮助。

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');

}

因为我想在小部件上使用它,所以我返回本地小写,但输出有时是一个数学结果,无法以这种方式返回。 为了解决这个问题,我在前面放了一个 : ,以便输出是文本而不是数学结果。 它解决了这个问题,但现在小部件总是输出 :nn 而不仅仅是 nn 。 这没什么大不了的,我现在只想知道如何正确地完成它。

如果我胡言乱语,我很抱歉。 我真的不知道自己在做什么。

javascript date time widget countdown
1个回答
0
投票

OP代码比较复杂,难以阅读。据我所知,它应该计算距离发薪日的天数,发薪日应该是该月的 25 日,或者如果是星期六或星期日,则为上一个星期五。

OP 代码中的问题太多,无法逐一解决。请参阅 在 JavaScript 中获取 2 个日期之间的差异? 以及某些问题的链接副本。这是基于上述规则的简单解决方案。

/* Return next pay day.
 *
 * If defaultPayDate falls on Sat or Sun, set payDate to prior Fri
 * If payDate is after baseDate, get next month's pay date
 *
 * @param {number} defaultPayDate - day of month for pay day
 * @param {Date} baseDate - date to calculate days to pay day from
 * @returns {Date} - next pay day
 */
function getNextPayday(defaultPayDate = 25, baseDate = new Date()) {

  // Get payday for the baseDate month
  let payDate = new Date(baseDate.getFullYear(), baseDate.getMonth(), defaultPayDate);
  
  // If on Sat (6) or Sun (0), shift to prior Fri
  if (!payDate.getDay() % 6) {
    // Subtract 2 from Sun, 1 from Sat
    payDate.setDate(payDate.getDate() - (payDate.getDay() + 2 % 7));
  }
  
  // If baseDate is after payDate, get next month's pay day
  if (payDate.getDate() < baseDate.getDate()) {
    payDate = getNextPayday(defaultPayDate, new Date(baseDate.getFullYear(), baseDate.getMonth() + 1, 1));
  }
  
  return payDate;
}

/* Days to next payday is not inclusive. Requires getNextPayday
 *
 * @param {number} defaultPayDate - day of month for pay day
 * @param {Date} baseDate - date to calculate days to pay day from
 * @returns {number} - days to next pay day (integer)
 */
function getDaysToNextPay(defaultPayDate = 25, baseDate = new Date()) {
  let payDate = getNextPayday(defaultPayDate, baseDate);
  // Use UTC to get differnce in days as UTC days are always exactly 8.64e7 ms long
  let daysToPayday = (Date.UTC(payDate.getFullYear(), payDate.getMonth(), payDate.getDate()) - 
                      Date.UTC(baseDate.getFullYear(), baseDate.getMonth(), baseDate.getDate())) / 8.64e7;
  
  return daysToPayday;
}

// Example
let d = new Date();

console.log('Today is           : ' + d.toDateString());
console.log('Next payday is     : ' + getNextPayday(25, d).toDateString()); 
console.log('Days to next payday: ' + getDaysToNextPay(25, d));

© www.soinside.com 2019 - 2024. All rights reserved.