String.to_datetime不使用当前时区(当没有给定时) [英] String.to_datetime does not use the current timezone (when none given)

查看:127
本文介绍了String.to_datetime不使用当前时区(当没有给定时)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,Rails的 String.to_timezone 方法不使用当前时区,当没有时区在字符串中。

I noticed that Rails' String.to_timezone method does not use the current time zone, when no time zone is given in the string.

$ "2013-01-14 14:38".to_datetime
=> Mon, 14 Jan 2013 14:38:00 +0000
$ DateTime.now
=> Mon, 14 Jan 2013 14:39:50 +0100

有没有办法告诉方法使用当前时区?

Is there a way to tell the method to use the current time zone?

谢谢。

推荐答案

Time.zone.parse('2013-01-14 14:38').to_datetime

这将使用当前时区来解析到目前为止的字符串。

This will use the current timezone to parse the string to date.

这篇关于String.to_datetime不使用当前时区(当没有给定时)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆