如何使用 ruby​​ 转换时间戳 [英] How to convert timestamp with ruby

查看:149
本文介绍了如何使用 ruby​​ 转换时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以用 ruby​​ 转换时间?我的程序打印时间戳是这样的:10/18/2107 14:08:09,但我希望它是这样的:2017 M10 18, Wed 14:08:09 GMT+02:00.据我了解,我想要的时间戳来自 Joda-Time.

Is it possible to convert time with ruby? My program prints the time stamp like this: 10/18/2107 14:08:09, but I want it to be like this: 2017 M10 18, Wed 14:08:09 GMT+02:00. From what I understand, the time stamp I want comes in Joda-Time.

有人可以帮我吗?

推荐答案

可以从字符串解析到 DateTime 传递格式,然后格式化输出字符串:

From the string you can parse it to DateTime passing the format, and then format the output string:

date_time = '10/18/2107 14:08:09'

puts DateTime.strptime(date_time, '%m/%d/%Y %H:%M:%S')
             .strftime('%Y M%m %d, %a %H:%M:%S %Z')

注意现在是星期二,时区指定为 00:00.

Note it's Tuesday and the time zone it's specified as 00:00.

这篇关于如何使用 ruby​​ 转换时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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