Ruby DateTime strptime()函数
DateTime#strptime() : strptime()是DateTime类的一个方法,它用给定的模板解析给定的日期和时间表示法
语法。DateTime.strptime()
参数。日期时间值
返回:用给定的模板解析给定的日期和时间表示法。
例子 #1 :
# Ruby code for DateTime.strptime() method
# loading library
require 'date'
# declaring DateTime value
date_a = DateTime.strptime('10-02-2015 05:05:06 PM', '%d-%m-%Y %I:%M:%S %p')
# declaring DateTime value
date_b = DateTime.strptime('2018 02 3 07 10 09 +7', '%Y %U %w %H %M %S %z')
# strptime method
puts "DateTime strptime form : #{date_a}\n\n"
puts "DateTime strptime form : #{date_b}\n\n"
输出:
DateTime strptime form : 2015-02-10T17:05:06+00:00
DateTime strptime form : 2018-01-17T07:10:09+07:00
例子 #2 :
# Ruby code for DateTime.strptime() method
# Ruby code for DateTime.strptime() method
# loading library
require 'date'
# declaring DateTime value
date_a = DateTime.strptime('2010 04 6 04 05 06 +7', '%Y %U %w %H %M %S %z')
# declaring DateTime value
date_b = DateTime.strptime('-1', '%s')
# strptime method
puts "DateTime strptime form : #{date_a}\n\n"
puts "DateTime strptime form : #{date_b}\n\n"
输出:
DateTime strptime form : 2010-01-30T04:05:06+07:00
DateTime strptime form : 1969-12-31T23:59:59+00:00