在Ruby中获取月中和月末的日期

时间:2014-04-15 16:55:57

标签: ruby date

我正在尝试编写工资单计划,其中一种付款方式是每月两次(15日和30日和/或31日)。我能够获得当月的付款日期,但我需要在下一年打印出来。到目前为止我的代码是:

require "active_support/core_ext"
require 'date'

def twice_a_month_payment_schedule
  middle_of_month = Date.today.at_end_of_month - 15
  end_of_month = Date.today.at_end_of_month
  one_year = Date.today.at_end_of_month + 365
  starting_payment_date = Date.parse(start_date) ### start_date is an argument in the format of "%Y-%m-%d".

  while starting_payment_date < one_year
    if middle_of_month.saturday?
      middle_of_month - 1
    elsif middle_of_month.sunday?
      middle_of_month - 2
    elsif end_of_month.saturday?
      end_of_month - 1
    elsif end_of_month.sunday?
      end_of_month - 2
    end
    starting_payment_date += 1
  end
  pay_dates << middle_of_month.strftime("%m-%d-%Y")
  pay_dates << end_of_month.strftime("%m-%d-%Y")
  pay_dates
end

任何建议都将不胜感激!

由于

2 个答案:

答案 0 :(得分:2)

为什么不概括助手来获取任何年/月的日期:

require "active_support/core_ext"
require 'date'

def twice_a_month_payment_schedule(year, month)
  base = Date.new(year, month)
  middle_of_month = 14.days.since(base)
  end_of_month = base.at_end_of_month

  if middle_of_month.saturday?
    middle_of_month - 1
  elsif middle_of_month.sunday?
    middle_of_month - 2
  elsif end_of_month.saturday?
    end_of_month - 1
  elsif end_of_month.sunday?
    end_of_month - 2
  end

  [middle_of_month.strftime("%m-%d-%Y"), end_of_month.strftime("%m-%d-%Y")]
end

puts "Jan 2014: #{twice_a_month_payment_schedule(2014, 1)}"
puts "Mar 2014: #{twice_a_month_payment_schedule(2014, 3)}"
puts "Aug 2014: #{twice_a_month_payment_schedule(2014, 8)}"
puts "May 2015: #{twice_a_month_payment_schedule(2015, 5)}"
puts "Feb 2024: #{twice_a_month_payment_schedule(2024, 2)}"
puts "Dec 2135: #{twice_a_month_payment_schedule(2135, 12)}"

结果:

⚡️ ruby derp.rb 
Jan 2014: ["01-15-2014", "01-31-2014"]
Mar 2014: ["03-15-2014", "03-31-2014"]
Aug 2014: ["08-15-2014", "08-31-2014"]
May 2015: ["05-15-2015", "05-31-2015"]
Feb 2024: ["02-15-2024", "02-29-2024"]
Dec 2135: ["12-15-2135", "12-31-2135"]

答案 1 :(得分:1)

这是评论,而不是答案。我想指出你的代码的一个小问题,并建议你如何组织它更好一点。我已经使用了@ Nick的代码并进行了一些更改。

我知道你不希望发薪日在周末降临。因此,而不是:

if middle_of_month.saturday?
  middle_of_month - 1
elsif middle_of_month.sunday?
  middle_of_month - 2
elsif end_of_month.saturday?
  end_of_month - 1
elsif end_of_month.sunday?
  end_of_month - 2
end

你想要:

if middle_of_month.saturday?
  middle_of_month - 1
elsif middle_of_month.sunday?
  middle_of_month - 2
end

if end_of_month.saturday?
  end_of_month - 1
elsif end_of_month.sunday?
  end_of_month - 2
end

目前看,如果两个发薪日最初都是在一个周末,那么只有月中发薪日会发生变化。通过这种修改,每个发薪日的操作几乎相同,因此消除重复位是有意义的。以下是您的表现方式:

require "active_support/core_ext"
require 'date'

def twice_a_month_payment_schedule(year, month)
  base = Date.new(year, month)
  [check_and_format_payday(base+14),
   check_and_format_payday(base.at_end_of_month)]
end

def check_and_format_payday(payday)
  case
  when payday.saturday?
    payday -= 1
  when payday.sunday?
    payday -= 2
  end
  payday.strftime("%a %m-%d-%Y")
end

puts "Jan 2014: #{twice_a_month_payment_schedule(2014, 1)}"
#  Jan 2014: ["Wed 01-15-2014", "Fri 01-31-2014"]
"Feb 2014: #{twice_a_month_payment_schedule(2014, 2)}"
#  Feb 2014: ["Fri 02-14-2014", "Fri 02-28-2014"]
"Mar 2014: #{twice_a_month_payment_schedule(2014, 3)}"
#  Mar 2014: ["Fri 03-14-2014", "Mon 03-31-2014"]
puts "Aug 2014: #{twice_a_month_payment_schedule(2014, 8)}"
#  Aug 2014: ["Fri 08-15-2014", "Fri 08-29-2014"]
puts "May 2015: #{twice_a_month_payment_schedule(2015, 5)}"
#  May 2015: ["Fri 05-15-2015", "Fri 05-29-2015"]
puts "Feb 2024: #{twice_a_month_payment_schedule(2024, 2)}"
#  Feb 2024: ["Thu 02-15-2024", "Thu 02-29-2024"]
puts "Dec 2135: #{twice_a_month_payment_schedule(2135, 12)}"
#  Dec 2135: ["Thu 12-15-2135", "Fri 12-30-2135"]

请注意,我稍微更改了输出格式以包含星期几。

require "active_support/core_ext"
只有方法at_end_of_month才需要

。您也可以自己定义它:

class Date
  def at_end_of_month
   next_month - 1
  end
end