Time Format Conversions

Hi all,

So, I’m trying to find out the best practice for converting a time
format from the following:

“3:00 PM”
“10:00 PM”
“1:00 AM”

TO:

“15:00:00”
“22:00:00”
“01:00:00”

Can anyone help me out on what I need to do here? I’m pulling strings
that need to be parsed and converted from > to the listings above.

Any help would be appreciated.

Many thanks in advance.

JD

gem install chronic

Chronic.parse(“3:00 PM”).strftime(’%H:%M:%S’)
=> “15:00:00”

Does this work?

Oops.

$ gem install chronic
$ irb

require ‘chronic’
Chronic.parse(“3:00 PM”).strftime(’%H:%M:%S’)
=> “15:00:00”

Thanks Steve,

That worked perfectly.

Steve R. wrote:

Oops.

$ gem install chronic
$ irb

require ‘chronic’
Chronic.parse(“3:00 PM”).strftime(’%H:%M:%S’)
=> “15:00:00”

The standard library ‘time’ parses this too:

require ‘time’
puts Time.parse(“3:00 PM”).strftime(’%H:%M:%S’)
=> 15:00:00

hth,

Siep

Thanks again fellas.

At 2009-08-31 02:09PM, “s.ross” wrote:

“15:00:00”
“22:00:00”
“01:00:00”

$ gem install chronic
$ irb

require ‘chronic’
Chronic.parse(“3:00 PM”).strftime(’%H:%M:%S’)
=> “15:00:00”

Don’t need a gem, the ‘time’ package from the standard library will do:

require 'time'
new = Time.parse(old).strftime("%T")

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs