Ruby:将小数形式的日期转换为名称形式的日期

发布于 2024-12-07 00:44:14 字数 434 浏览 0 评论 0原文

是否可以将 strftime("%u") 值快速转换为 strftime("%A") 或者我是否需要构建一个等价哈希,例如 {"Monday" => 1、……“周日”=> 6}

我有一个数组,其中某天为十进制值

class_index=[2,6,7]

,我想循环遍历这个数组来构建像这样的日期名称数组,

[nil, "Tuesday", nil, nil, nil, "Saturday", "Sunday"]

这样我就可以

class_list=[]
class_index.each do |x|
  class_list[x-1] = convert x value to day name
end

这样做这可能吗?

Is it possible to convert quickly a strftime("%u") value to a strftime("%A") or do i need to build an equivalence hash like {"Monday" => 1, ......... "Sunday" => 6}

I have an Array with some day as decimal values

class_index=[2,6,7]

and I would like to loop through this array to build and array of days name like this

[nil, "Tuesday", nil, nil, nil, "Saturday", "Sunday"]

so I could do

class_list=[]
class_index.each do |x|
  class_list[x-1] = convert x value to day name
end

Is that even possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

东北女汉子 2024-12-14 00:44:14

怎么样:

require "date"
DateTime.parse("Wednesday").wday # => 3

哦,我现在看到你扩展了你的问题。怎么样:

[2,6,7].inject(Array.new(7)) { |memo,obj| memo[obj-1] = Date::DAYNAMES[obj%7]; memo }

让我解释一下:

input = [2,6,7]
empty_array = Array.new(7) # => [nil, nil, nil, nil, nil, nil, nil]
input.inject(empty_array) do |memo, obj| # loop through the input, and
                                         # use the empty array as a 'memo'
  day_name = Date::DAYNAMES[obj%7]       # get the day's name, modulo 7 (Sunday = 0)
  memo[obj-1] = day_name                 # save the day name in the empty array
  memo                                   # return the memo for the next iteration
end

Ruby 的美丽。

How about:

require "date"
DateTime.parse("Wednesday").wday # => 3

Oh, I now see you've expanded your question. How about:

[2,6,7].inject(Array.new(7)) { |memo,obj| memo[obj-1] = Date::DAYNAMES[obj%7]; memo }

Let me explain that one:

input = [2,6,7]
empty_array = Array.new(7) # => [nil, nil, nil, nil, nil, nil, nil]
input.inject(empty_array) do |memo, obj| # loop through the input, and
                                         # use the empty array as a 'memo'
  day_name = Date::DAYNAMES[obj%7]       # get the day's name, modulo 7 (Sunday = 0)
  memo[obj-1] = day_name                 # save the day name in the empty array
  memo                                   # return the memo for the next iteration
end

The beauty of Ruby.

极致的悲 2024-12-14 00:44:14

要从十进制转为工作日:

require 'date'
Date::DAYNAMES[1]
# => "Monday"

因此,在您的示例中,您可以简单地执行以下操作:

class_list=[]
class_index.each do |x|
  class_list[x-1] = Date::DAYNAMES[x-1]
end

To go from decimal to weekday:

require 'date'
Date::DAYNAMES[1]
# => "Monday"

So, in your example, you could simply do:

class_list=[]
class_index.each do |x|
  class_list[x-1] = Date::DAYNAMES[x-1]
end
(り薆情海 2024-12-14 00:44:14

我想到了一种方法:

require "date"

def weekday_index_to_name(index)
  date = Date.parse("2011-09-26") # Canonical Monday.
  (index - 1).times { date = date.succ }
  date.strftime("%A")
end

Here’s one way that comes to mind:

require "date"

def weekday_index_to_name(index)
  date = Date.parse("2011-09-26") # Canonical Monday.
  (index - 1).times { date = date.succ }
  date.strftime("%A")
end
圈圈圆圆圈圈 2024-12-14 00:44:14
class_index=[2,6,7]

class_index.map{|day_num| Date::DAYNAMES[day_num%7]}

#=> ["Tuesday", "Saturday", "Sunday"]

请注意,日期名称是从 0 到 6,因此您可以从 0 到 6 进行运算,也可以对 7 求模

class_index=[2,6,7]

class_index.map{|day_num| Date::DAYNAMES[day_num%7]}

#=> ["Tuesday", "Saturday", "Sunday"]

note that day names are from 0 to 6, so you can either work from 0 to 6 or have it modulo 7

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文