DateTime string in Ruby - how to make the time optional -
is there way parse timestamp string of format "dd/mm/yyyy hh:mm:ss" without cycling through several date-format strings, time optional.
wishful-thinking examples:
"01/01/2014" -> 01/01/2014 00:00:00 "01/01/2014 10:00" -> 01/01/2014 10:00:00 tried use datetime.strptime("01/01/14 12:00", "%d/%m/%y %h:%m:%s") got error because there no seconds.
assuming dates trying parse in format %d/%m/%y %h:%m:%s , possibly missing hours, minutes, or seconds, datetime.parse method should do:
require 'date' datetime.parse("01/01/2014").to_s # => "2014-01-01t00:00:00+00:00" datetime.parse("01/01/2014 10:00").to_s # => "2014-01-01t10:00:00+00:00" datetime.parse("01/01/2014 10:00:30").to_s # => "2014-01-01t10:00:30+00:00"
Comments
Post a Comment