gpt4 book ai didi

Ruby:中文解析时间

转载 作者:太空宇宙 更新时间:2023-11-03 18:09:39 25 4
gpt4 key购买 nike

时间是这样的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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com