Ruby DateTime hour()函数
DateTime#hour() : hour() 是一个DateTime类的方法,它可以从给定的DateTime对象中返回小时数,即从0到23。
语法。DateTime.hour()
参数。DateTime值
返回:小时,即从给定的DateTime对象中的0到23小时。
例子 #1 :
# Ruby code for DateTime.hour() method
# loading library
require 'date'
# declaring DateTime value
date_a = DateTime.new(2019, 8, 10, 4, 10, 9)
# declaring DateTime value
date_b = DateTime.new(2019, 8, 10.5)
# declaring DateTime value
date_c = DateTime.new(2019, 8, 10, 4, 10, 9, Rational(4, 24))
# hour method
puts "DateTime hour form : #{date_a.hour}\n\n"
puts "DateTime hour form : #{date_b.hour}\n\n"
puts "DateTime hour form : #{date_c.hour}\n\n"
输出:
DateTime hour form : 4
DateTime hour form : 12
DateTime hour form : 4
例子 #2 :
# Ruby code for DateTime.hour() method
# loading library
require 'date'
# declaring DateTime value
date_a = DateTime.new(2019, 8, 10, 5)
# declaring DateTime value
date_b = DateTime.parse('10 Aug 2018 04:10:06+04:30')
# declaring DateTime value
date_c = DateTime.new(2019, 8, 10, 4, 10, 9, '+03:00')
# hour method
puts "DateTime hour form : #{date_a.hour}\n\n"
puts "DateTime hour form : #{date_b.hour}\n\n"
puts "DateTime hour form : #{date_c.hour}\n\n"
输出:
DateTime hour form : 5
DateTime hour form : 4
DateTime hour form : 4