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?
Thanks.
Try this:
Time.zone.parse('2013-01-14 14:38').to_datetime
This will use the current timezone to parse the string to date.
There's #in_time_zone method on the DateTime
object you can use:
"2013-01-14 14:38".to_datetime.in_time_zone
"2013-01-14 14:38".to_datetime.in_time_zone("Prague")
Use in_time_zone
:
"2013-01-14 16:06".in_time_zone
=> Mon, 14 Jan 2013 16:06:00 CET +01:00
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With