简体   繁体   English

日期范围基于Date.today

[英]Date range based on Date.today

I have an app which is used to calculate the paychecks of a number of employees. 我有一个应用程序,用于计算许多员工的薪水。

The paychecks are calculated from the 21st in a month to the 20th in the next month. 薪水从一个月的21号到下个月的20号进行计算。 So when the employee uses the app I need to find these two dates (start_date and end_date) based on the date today. 因此,当员工使用该应用程序时,我需要根据今天的日期查找这两个日期(开始日期和结束日期)。

How can I do that? 我怎样才能做到这一点? Something like beginning_of_month + 21 doesn't work because when it's the 3rd in a month it's beginning_of_last_month.. 诸如beginning_of_month + 21之类的东西不起作用,因为当它是一个月中的第三个月时,它是starting_of_last_month。

Any simple way to find the last 21st date and the next 20th date ? 有什么简单的方法可以找到最后一个21日和下一个20日?

You could create a helper method that could look something like this: 您可以创建一个类似于以下内容的辅助方法:

def paycheck_dates(date, past_day, future_day)
    before, after = Date.civil(date.year, date.month, past_day), Date.civil(date.year, date.month, future_day)
    before <<= 1 if before > date
    after >>= 1 if after < date
    return before, after
end

The operator <<= substracts a month, and >>= adds a month. 运算符<<=减去一个月, >>=加上一个月。 In your case you could call this method like this: 在您的情况下,您可以这样调用此方法:

current_date = Date.today
past_date, future_date = paycheck_dates(current_date, 21, 20)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM