Ruby中的DateTime.parse()是否依赖于语言环境? [英] Is DateTime.parse() in Ruby dependent on locale?
问题描述
我想知道以下示例的输出:
解析 01/03
时,将其解析为 Mar ,1st
或 Jan,3rd
?
I'm wondering regarding the output of the following example:
when parsing 01/03
, will it be resolved as Mar, 1st
or Jan, 3rd
?
推荐答案
< Ruby不依赖于语言环境。由于Ruby是服务器端语言,而不是JavaScript等客户端语言,因此Ruby使用来自您的网络应用服务器的系统时钟-并使用此信息来计算时间。
Ruby is not locale dependent. Because Ruby is a server-side language and not a client-side language like JavaScript, Ruby uses the system clock from your web app server - and uses this information to calculate the time. Whatever timezone you have your system set to is what your Ruby app will use.
从字符串中解析日期时, DateTime
将根据输入的格式进行最佳猜测:
When parsing a date from a string, DateTime
will make its best guess based on how the input is formatted:
DateTime.parse('01/03')
#=> Thu, 03 Jan 2019 00:00:00 +0000
DateTime.parse('2019/01/03')
#=> Thu, 03 Jan 2019 00:00:00 +0000
DateTime.parse('01/03/2019')
#=> Fri, 01 Mar 2019 00:00:00 +0000
您也可以明确地 tell DateTime
如何使用 strptime
解析字符串:
You can also explicitly tell DateTime
how you want your string parsed using strptime
:
date = '01-03-2019'
DateTime.strptime(date, '%m-%d-%Y')
#=> Thu, 03 Jan 2019 00:00:00 +0000
DateTime.strptime(date, '%d-%m-%Y')
#=> Fri, 01 Mar 2019 00:00:00 +0000
这篇关于Ruby中的DateTime.parse()是否依赖于语言环境?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!