将时间转换为其他时区 [英] Convert time to other timezone

查看:45
本文介绍了将时间转换为其他时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这种格式的日期字符串yyyy-mm-ddThh:mm:ss[Z]

I have a datestring in this format yyyy-mm-ddThh:mm:ss[Z]

我有一个时区字符串.例如亚洲/加尔各答"

And i have a timezone string. for e.g "Asia/Kolkata"

现在我想将此日期字符串转换为给定时区的时区

Now i want to convert this date string into the timezone of the given timezone

例如如果日期是 2014-01-03T23:30:00Z ,那么在亚洲/加尔各答"时区,它将是 2014-01-04T05:00:00 .

for e.g. if the date is 2014-01-03T23:30:00Z , then in "Asia/Kolkata" timezone it will be 2014-01-04T05:00:00 .

我尝试使用 Time library ,但 Time library 似乎没有任何方法可以转换为其他 timzone http://ruby-doc.org/core-1.8.6/Time.html#method-c-mktime .

I tried using Time library , but Time library does not seem to have any method which can convert to other timzone http://ruby-doc.org/core-1.8.6/Time.html#method-c-mktime .

推荐答案

您应该使用 TZInfo gem.

You should use the TZInfo gem.

require 'tzinfo'

tz = TZInfo::Timezone.get('Asia/Kolkata')
utc = DateTime.iso8601('2014-01-03T23:30:00Z')
local = tz.utc_to_local(utc)

这篇关于将时间转换为其他时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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