时间是这样的2016年4月27日
(年月日)
什么是解析它的好方法?
我想我可以逃过一劫
time = '2016年4月27日'
year = time.split('年')[0]
然后重新安排时间。但这还涉及编码utf-8
的东西,我不是很熟悉。
最佳答案
假设 '2016 年 4 月 27 日'
是您的输入字符串并且您希望从中得到一个 Date
对象,因为您知道字符串的每个组成部分是什么代表,你可以使用 Date.strptime
并传入格式字符串:
irb> date = Date.strptime("2016年4月27日", "%Y年%m月%d日")
=> #<Date: 2016-04-27 ((2457506j,0s,0n),+0s,2299161j)>
irb> date.year
=> 2016
irb> date.month
=> 4
irb> date.day
=> 27
关于Ruby:中文解析时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36963676/