【问题标题】:Ruby - I need to convert string into a timestampRuby - 我需要将字符串转换为时间戳
【发布时间】:2016-02-18 19:08:26
【问题描述】:

我需要将表示日期的字符串转换为 Ruby 中的时间戳对象。

例如:

   date_string = "18-Feb-2016 09:01:04"

   convert to a timestamp like so

   2016-02-18 14:01:04

如果列是时间戳类型,我需要将其保存到 mysql 数据库中。

我已经研究了一天的大部分时间,但找不到解决方案。我知道您可以使用 Time.parse 但包括时区和 DateTime.parse().to_time 包括时区。由于它必须是时间戳,我不能使用 strftime 方法。

我需要时间加入,因为它将用于计算目的。

任何帮助将不胜感激。

谢谢

【问题讨论】:

  • “时间戳”是什么意思?您说您想要一个“时间戳object”,但2016-02-18 14:01:04 似乎不是Time 类的实例。还是您想通过从Time 对象中提取信息来显示的字符串(在这种情况下需要用引号括起来)?
  • 0914 的小时是怎么过去的?
  • 这应该可以解决问题:DateTime.parse("18-Feb-2016 09:01:04").to_s(:db)(请参阅下面的答案)

标签: ruby-on-rails ruby


【解决方案1】:

TL;DR

datetime = DateTime.parse("18-Feb-2016 09:01:04").to_s(:db)

返回

"2016-02-18 09:01:04"

这里有一个简单的解释......

1.使用 DateTime.parse

将您的字符串转换为 Date 对象

您可以使用DateDateTime 类中的.parse 方法来解析字符串。 parse 方法将返回一个 Date 对象,如下所示:

$ DateTime.parse("18-Feb-2016 09:01:04")
$ => #<DateTime: 2016-02-18T09:01:04+00:00 ((2457437j,32464s,0n),+0s,2299161j)>

.parse 是 Ruby 提供的一个方法。

2。将字符串格式化为DateTime.parse.to_s

Ruby on Rails 让您可以访问DateTime.to_formatted_s 方法来更改 Date 对象的格式,然后再将其存储到数据库中。

要匹配您指定的格式:

$ datetime = DateTime.parse("18-Feb-2016 09:01:04").to_formatted_s

注意:to_sto_formatted_s 的别名,to_formatted_s 是 Rails 提供的方法,而不是 Ruby。

【讨论】:

    【解决方案2】:

    在 Rails 中使用 to_datetime 方法。

    "12-10-2015".to_datetime
    

    => 2015 年 10 月 12 日星期一 10:36:00 +0000

    http://apidock.com/rails/String/to_datetime

    已编辑以添加准确的答案。

    【讨论】:

    • 嗨。感谢您的回复,但这似乎是一种不适用于普通 ruby​​ 的 rails 方法。
    • 对不起,@amit sharma 的回答似乎很有希望。
    【解决方案3】:

    您可以使用.to_time.to_datetime.to_time 返回带有时区的日期和时间,但 .to_datetime 返回带有周名称的完整日期,但它显示 +0000 作为时区,您会看到不同两种格式,请参见以下示例。

    # used .to_time
    
    "18-Feb-2016 09:01:04".to_time
    ## Output
    2016-02-18 09:01:04 +0530
    
    # used .to_datetime
    
    "18-Feb-2016 09:01:04".to_datetime
    ## Output
    Thu, 18 Feb 2016 09:01:04 +0000
    

    【讨论】:

      【解决方案4】:

      我将问题解释为您希望将字符串 "18-Feb-2016 09:01:04" 转换为字符串 "2016-02-18 14:01:04"(当然,泛化为任意日期时间字符串)。

      让:

      str = "18-Feb-2016 09:01:04"
      

      您想要的事情分两步完成。首先是将这个字符串转换为DateTime对象,即DateTime类的一个实例。第二步是从DateTime 对象构造所需的字符串。

      创建DateTime 对象的一种方法是使用DateTime::parse 方法:

      require 'date'
      
      DateTime.parse(str)
        #=> #<DateTime: 2016-02-18T09:01:04+00:00 ((2457437j,32464s,0n),+0s,2299161j)> 
      

      这适用于您提供的字符串格式,但对于其他格式可能会出现问题。例如:

      DateTime.parse "4-5-16 09:01:04"
        #=> #<DateTime: 2004-05-16T09:01:04+00:00 ((2453142j,32464s,0n),+0s,2299161j)> 
      

      只要您知道将使用的格式,通常最好使用DateTime#strptime 和由格式指令组成的适当模式:

      pattern = "%d-%m-%y %H:%M:%S"
      DateTime.strptime("4-5-16 09:01:04", pattern)
        #=> #<DateTime: 2016-05-04T09:01:04+00:00((2457513j,32464s,0n),+0s,2299161j)> 
      

      有关格式指令,请参阅DateTime#strftime

      对于手头的问题:

      dt = DateTime.strptime(str, "%d-%b-%Y %H:%M:%S")
        #=> #<DateTime: 2016-02-18T09:01:04+00:00 ((2457437j,32464s,0n),+0s,2299161j)>
      

      第二步,用上面引用的strftime方法构造想要的字符串:

      dt.strftime("%Y-%m-%d %H:%M:%S")
        #=> "2016-02-18 09:01:04"
      

      【讨论】:

        猜你喜欢
        • 2020-11-22
        • 2019-04-16
        • 1970-01-01
        • 1970-01-01
        • 2016-11-30
        • 2017-07-29
        • 2013-09-25
        • 2021-09-08
        • 1970-01-01
        相关资源
        最近更新 更多