红宝石 |日期时间 strptime()函数
DateTime#strptime() : strptime()是一个 DateTime 类方法,它使用给定模板解析给定的日期和时间表示
Syntax: DateTime.strptime()
Parameter: DateTime values
Return: parses the given representation of date and time with the given template
示例 #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