在Ruby中将日期字符串分解为单独的span元素
我正在寻找从MySQL获取datetime字符串的最佳方法,用Ruby将其分解,并在单独的元素中返回月份、日期和年份。字符串的格式如何?您可以将datetime字符串转换为datetime对象,并调用实例方法在Ruby中将日期字符串分解为单独的span元素,ruby,datetime,Ruby,Datetime,我正在寻找从MySQL获取datetime字符串的最佳方法,用Ruby将其分解,并在单独的元素中返回月份、日期和年份。字符串的格式如何?您可以将datetime字符串转换为datetime对象,并调用实例方法 require 'time' x = "2009/04/16 19:52:30" #grab your datetime string from the database and assign it y = DateTime.strptime(x, "%Y/%m/%d %H:%M:%S"
require 'time'
x = "2009/04/16 19:52:30" #grab your datetime string from the database and assign it
y = DateTime.strptime(x, "%Y/%m/%d %H:%M:%S") #create a new date object
然后,一个简单的y.day()产生:
和y.hour()
仅供参考,我实际上从来没有太多地使用过Ruby,所以这就是我从玩控制台中得到的,所以希望这有助于了解一些情况。字符串的格式如何?您可以将datetime字符串转换为datetime对象,并调用实例方法
require 'time'
x = "2009/04/16 19:52:30" #grab your datetime string from the database and assign it
y = DateTime.strptime(x, "%Y/%m/%d %H:%M:%S") #create a new date object
require 'time'
x = "2009/04/16 19:52:30"
begin
y = Time.parse(x)
[y.year, y.month, y.day] # => [2009, 4, 16]
rescue ArgumentError
puts "cannot parse date: #{x}"
end
然后,一个简单的y.day()产生:
和y.hour()
仅供参考,我实际上从来没有太多地使用过Ruby,所以这就是我从玩控制台中得到的,所以希望这能帮助我们了解一些情况
require 'time'
x = "2009/04/16 19:52:30"
begin
y = Time.parse(x)
[y.year, y.month, y.day] # => [2009, 4, 16]
rescue ArgumentError
puts "cannot parse date: #{x}"
end