Ruby:将小数形式的日期转换为名称形式的日期
是否可以将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
怎么样:
哦,我现在看到你扩展了你的问题。怎么样:
让我解释一下:
Ruby 的美丽。
How about:
Oh, I now see you've expanded your question. How about:
Let me explain that one:
The beauty of Ruby.
要从十进制转为工作日:
因此,在您的示例中,您可以简单地执行以下操作:
To go from decimal to weekday:
So, in your example, you could simply do:
我想到了一种方法:
Here’s one way that comes to mind:
请注意,日期名称是从 0 到 6,因此您可以从 0 到 6 进行运算,也可以对 7 求模
note that day names are from 0 to 6, so you can either work from 0 to 6 or have it modulo 7