2

我正在尝试转换为 Mysql 时间日期时间格式,但秒不转换,只有日期看起来不错。

1.9.3-p448 :030 >  d = Date.strptime "26 Jul 2013 00:04:52 UTC", '%d %b %Y %H:%M:%S %z'
=> #<Date: 2013-07-26 ((2456500j,0s,0n),+0s,2299161j)>
1.9.3-p448 :031 > d.strftime ("%Y-%m-%d %H:%M:%S")
=> "2013-07-26 00:00:00"
4

3 回答 3

4

您正在使用 a Date,它没有时间。尝试使用DateTime

irb [1.9.3]$ require "date"
=> true
irb [1.9.3]$ d = DateTime.strptime "26 Jul 2013 00:04:52 UTC", '%d %b %Y %H:%M:%S'
=> #<DateTime: 2013-07-26T00:04:52+00:00 ((2456500j,292s,0n),+0s,2299161j)>
irb [1.9.3]$  d.strftime ("%Y-%m-%d %H:%M:%S")
=> "2013-07-26 00:04:52"
于 2013-08-15T05:25:21.927 回答
2

您必须使用Date的Time。CauseDate仅包含一个日期,例如YYYY-MM-DD.

例子:

root@ubuntu-server:~# irb
irb(main):001:0> require 'time'
=> true
irb(main):002:0> t = Time.parse("26 Jul 2013 00:04:52 UTC")
=> Fri Jul 26 00:04:52 UTC 2013
irb(main):003:0> t.strftime("%Y-%m-%d %H:%M:%S")
=> "2013-07-26 00:04:52"  
于 2013-08-15T05:29:18.697 回答
1

您使用了错误的类型使用DateTime

1.9.3-p448 :030 >  d = DateTime.strptime "26 Jul 2013 00:04:52 UTC", '%d %b %Y %H:%M:%S %z'
=> #<DateTime: 53060389273/21600,0,2299161>
1.9.3-p448 :031 > d.strftime ("%Y-%m-%d %H:%M:%S")
=> "2013-07-26 00:04:52"
于 2013-08-15T05:27:24.523 回答