我怎样才能得到每月的第15天和最后一天?

时间:2022-10-16 12:10:20

I'm using Ruby and Ruby on Rails 4.

我正在使用Ruby和Ruby on Rails 4。

I'd like get the dates for the 15th and last day of each month for the next two months.

我想要在接下来的两个月里,每个月的第15天和最后一天的日期。

start_date = Time.now
mid_month = start_date.15th.day.of.month
last_day_of_month = start.last.day.of.month

What's the easiest way to accomplish this using Ruby or Rails?

使用Ruby或Rails完成这个任务最简单的方法是什么?

5 个解决方案

#1


9  

I'm not sure if this will work with Rails 4:

我不确定这是否适用于Rails 4:

start_date = Date.today
  # => Wed, 06 Nov 2013
mid_month  = (start_date + 1.month).beginning_of_month + 14
  # => Sun, 15 Dec 2013
end_month  = (start_date + 1.month).end_of_month
  # => Tue, 31 Dec 2013

#2


1  

I think something below you are looking for :

我认为你在寻找以下的东西:

require 'date'

d = Date.today
start_date = d-d.mday
p date_15 = start_date.next_day(15)
# => #<Date: 2013-11-15 ((2456612j,0s,0n),+0s,2299161j)>
p date_last = d.next_month - d.mday
# => #<Date: 2013-11-30 ((2456627j,0s,0n),+0s,2299161j)>

#3


1  

For the NEXT two months:

接下来的两个月:

start_date = Date.today

[1..2].each do |month_add|
  mid_month  = (start_date + month_add.month).beginning_of_month + 14
  end_month  = (start_date + month_add.month).end_of_month
  puts 'Month: #{month_add}'
  puts "Mid Month "+mid_month
  puts "End Month "+end_month
end

#4


1  

Here's some more:

这里有一些更多的:

    start_date == Date.today

    beginning = start_date.beginning_of_month
    middle = beginning.advance(weeks:2)
    end = start_date.end_of_month

Looks more human readable.

看起来更人类可读的。

All these methods available from ActiveSupport class extensions, so you have to include it if not using Rails

所有这些方法都可以从ActiveSupport类扩展中获得,所以如果不使用Rails,就必须包含它们

#5


0  

There might be some easier way, but this comes to my mind:

也许有更简单的方法,但我想到了:

require 'Date'

start_date   = Date.today
mid_month    = Date.new(start_date.year, start_date.month, 15)
end_of_month = Date.new(start_date.year, start_date.month+1, 1)-1
#or
end_of_month = Date.new(start_date.year, start_date.month, 1).next_month.prev_day

#1


9  

I'm not sure if this will work with Rails 4:

我不确定这是否适用于Rails 4:

start_date = Date.today
  # => Wed, 06 Nov 2013
mid_month  = (start_date + 1.month).beginning_of_month + 14
  # => Sun, 15 Dec 2013
end_month  = (start_date + 1.month).end_of_month
  # => Tue, 31 Dec 2013

#2


1  

I think something below you are looking for :

我认为你在寻找以下的东西:

require 'date'

d = Date.today
start_date = d-d.mday
p date_15 = start_date.next_day(15)
# => #<Date: 2013-11-15 ((2456612j,0s,0n),+0s,2299161j)>
p date_last = d.next_month - d.mday
# => #<Date: 2013-11-30 ((2456627j,0s,0n),+0s,2299161j)>

#3


1  

For the NEXT two months:

接下来的两个月:

start_date = Date.today

[1..2].each do |month_add|
  mid_month  = (start_date + month_add.month).beginning_of_month + 14
  end_month  = (start_date + month_add.month).end_of_month
  puts 'Month: #{month_add}'
  puts "Mid Month "+mid_month
  puts "End Month "+end_month
end

#4


1  

Here's some more:

这里有一些更多的:

    start_date == Date.today

    beginning = start_date.beginning_of_month
    middle = beginning.advance(weeks:2)
    end = start_date.end_of_month

Looks more human readable.

看起来更人类可读的。

All these methods available from ActiveSupport class extensions, so you have to include it if not using Rails

所有这些方法都可以从ActiveSupport类扩展中获得,所以如果不使用Rails,就必须包含它们

#5


0  

There might be some easier way, but this comes to my mind:

也许有更简单的方法,但我想到了:

require 'Date'

start_date   = Date.today
mid_month    = Date.new(start_date.year, start_date.month, 15)
end_of_month = Date.new(start_date.year, start_date.month+1, 1)-1
#or
end_of_month = Date.new(start_date.year, start_date.month, 1).next_month.prev_day