ruby-on-rails 如何删除时间范围内的日期数组中的元素Rails?

h79rfbju  于 2023-02-26  发布在  Ruby
关注(0)|答案(2)|浏览(119)

我的数组是由

start_date = (Date.today + 10.hours + 30.minutes).to_datetime       
   end_date = (Date.today + 1.day + 18.hours).to_datetime
   period = 0.5/24 
   array = start_date.step(end_date, period).to_a

这是结果(但此结果将涵盖最多62天)

array = 
  [Sat, 25 Feb 2023 10:30:00 +0000, Sat, 25 Feb 2023 11:00:00 +0000, Sat, 25 Feb 2023 11:30:00 +0000, 
  Sat, 25 Feb 2023 12:00:00 +0000, Sat, 25 Feb 2023 12:30:00 +0000, Sat, 25 Feb 2023 13:00:00 +0000,
  Sat, 25 Feb 2023 13:30:00 +0000, Sat, 25 Feb 2023 14:00:00 +0000, Sat, 25 Feb 2023 14:30:00 +0000, 
  Sat, 25 Feb 2023 15:00:00 +0000, Sat, 25 Feb 2023 15:30:00 +0000, Sat, 25 Feb 2023 16:00:00 +0000, 
  Sat, 25 Feb 2023 16:30:00 +0000, Sat, 25 Feb 2023 17:00:00 +0000, Sat, 25 Feb 2023 17:30:00 +0000, 
  Sat, 25 Feb 2023 18:00:00 +0000, Sat, 25 Feb 2023 18:30:00 +0000, Sat, 25 Feb 2023 19:00:00 +0000, 
  Sat, 25 Feb 2023 19:30:00 +0000, Sat, 25 Feb 2023 20:00:00 +0000, Sat, 25 Feb 2023 20:30:00 +0000, 
  Sat, 25 Feb 2023 21:00:00 +0000, Sat, 25 Feb 2023 21:30:00 +0000, Sat, 25 Feb 2023 22:00:00 +0000, 
  Sat, 25 Feb 2023 22:30:00 +0000, Sat, 25 Feb 2023 23:00:00 +0000, Sat, 25 Feb 2023 23:30:00 +0000, 
  Sun, 26 Feb 2023 00:00:00 +0000, Sun, 26 Feb 2023 00:30:00 +0000, Sun, 26 Feb 2023 01:00:00 +0000, 
  Sun, 26 Feb 2023 01:30:00 +0000, Sun, 26 Feb 2023 02:00:00 +0000, Sun, 26 Feb 2023 02:30:00 +0000, 
  Sun, 26 Feb 2023 03:00:00 +0000, Sun, 26 Feb 2023 03:30:00 +0000, Sun, 26 Feb 2023 04:00:00 +0000, 
  Sun, 26 Feb 2023 04:30:00 +0000, Sun, 26 Feb 2023 05:00:00 +0000, Sun, 26 Feb 2023 05:30:00 +0000, 
  Sun, 26 Feb 2023 06:00:00 +0000, Sun, 26 Feb 2023 06:30:00 +0000, Sun, 26 Feb 2023 07:00:00 +0000, 
  Sun, 26 Feb 2023 07:30:00 +0000, Sun, 26 Feb 2023 08:00:00 +0000, Sun, 26 Feb 2023 08:30:00 +0000, 
  Sun, 26 Feb 2023 09:00:00 +0000, Sun, 26 Feb 2023 09:30:00 +0000, Sun, 26 Feb 2023 10:00:00 +0000, 
  Sun, 26 Feb 2023 10:30:00 +0000, Sun, 26 Feb 2023 11:00:00 +0000, Sun, 26 Feb 2023 11:30:00 +0000, 
  Sun, 26 Feb 2023 12:00:00 +0000, Sun, 26 Feb 2023 12:30:00 +0000, Sun, 26 Feb 2023 13:00:00 +0000, 
  Sun, 26 Feb 2023 13:30:00 +0000, Sun, 26 Feb 2023 14:00:00 +0000, Sun, 26 Feb 2023 14:30:00 +0000, 
  Sun, 26 Feb 2023 15:00:00 +0000, Sun, 26 Feb 2023 15:30:00 +0000, Sun, 26 Feb 2023 16:00:00 +0000, 
  Sun, 26 Feb 2023 16:30:00 +0000, Sun, 26 Feb 2023 17:00:00 +0000, Sun, 26 Feb 2023 17:30:00 +0000, 
  Sun, 26 Feb 2023 18:00:00 +0000]

从这个数组中,我想删除从每天的开始到10:29点之间以及从18:00点之后到每天结束之间的所有日期时间。
预期结果:

array = 
  [Sat, 25 Feb 2023 10:30:00 +0000, Sat, 25 Feb 2023 11:00:00 +0000, ....... Sat, 25 Feb 2023 18:00:00 +0000,
  Sun, 26 Feb 2023 10:30:00 +0000, Sun, 26 Feb 2023 11:00:00 +0000, ....... Sun, 26 Feb 2023 18:00:00 +0000]

我试过了

w_start_time = (start_date + 8.hours + 1.minute).strftime('%k:%M:%S')
 w_end_time = (start_date - 1.minute).strftime('%k:%M:%S')
        
 hours = array_of_datestimes.reject! { |n| n.strftime('%k:%M:%S').between?(w_start_time, w_end_time) }

这将返回nil
有人能帮帮我吗?
谢谢

beq87vna

beq87vna1#

也许你应该试试这个:

array = array.reject { |date| 
  (date.hour < 10 || (date.hour == 10 && date.min < 30)) || 
  (date.hour >= 18) 
}
ccrfmcuu

ccrfmcuu2#

这对我很有效

w_start_time = (start_date - 1.minute).strftime('%k:%M:%S')
 w_end_time = (start_date + 8.hours - 1.minute).strftime('%k:%M:%S')

 hours = array.filter { |n| n.strftime('%k:%M:%S').between?(w_start_time, w_end_time) }

相关问题