我将如何将此时间字符串转换为 ruby​​ 中的纪元时间? [英] How would I go about converting this time string to epoch time in ruby?

查看:33
本文介绍了我将如何将此时间字符串转换为 ruby​​ 中的纪元时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将如何将 2012-12-13T14:43:35.371Z 转换为纪元时间?

How would I go about converting 2012-12-13T14:43:35.371Z to epoch time?

我正在考虑的一种方法是去除 T 和 Z,然后尝试使用 DateTime 库找到一种转换为纪元的方法.在我走这条路之前,我很好奇是否有一个宝石可以用来快速完成这项工作.在相关说明中,我将在转换为纪元后进行时间计算.

One approach I was considering was to strip the T and Z and then try to use the DateTime library to find a way to convert to epoch. Before I go down that path, I was curious if there is a gem I could use to do this quickly. On a related note, I will be doing time calculations after I convert to epoch.

推荐答案

例如:

require 'date'

# as an integer:
DateTime.parse('2012-12-13T14:43:35.371Z').to_time.to_i
# or as a string:
DateTime.parse('2012-12-13T14:43:35.371Z').strftime('%s')

这篇关于我将如何将此时间字符串转换为 ruby​​ 中的纪元时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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