Ruby Datetime Parsing from 'Mm/Dd/Yyyy' Format

ruby DateTime parsing from 'mm/dd/yyyy' format

require 'date'
my_date = Date.strptime("12/22/2011", "%m/%d/%Y")

How to convert MM/DD/YYYY to time in ruby?

You should be able to send that string right into Time.parse like so:

2.0.0-p247 :001 > require 'time'
=> true

Before Ruby 1.9:

2.0.0-p247 :002 > Time.parse("12/1/2014 5:55PM")
=> 2014-12-01 17:55:00 -0500

After Ruby 1.9

2.0.0-p247 :002 > Time.strptime("12/1/2014 5:55PM","%m/%d/%Y %I:%M%p")
=> 2014-12-01 17:55:00 -0500

How to change date format from mm/dd/yyyy to dd/mm/yyyy Ruby on Rails

Two steps:

  • You need to convert your string into Date object. For that, use Date#strptime.
  • You can use Date#strftime to convert the Date object into preferred format.

See implementation below:

str = '01/14/2018'

date = Date.strptime(str, '%m/%d/%Y')
=> #<Date: 2018-01-14 ((2458133j,0s,0n),+0s,2299161j)>

date.strftime('%d-%m-%Y')
=> "14-01-2018"

date.strftime('%Y-%m-%d')
=> "2018-01-14"

How do I format a date to mm/dd/yyyy in Ruby?

The strftime method can be used to format times:

Time.now.strftime("%m/%d/%Y")

Formatting Date/Time in Ruby to YYYY-MM-DD HH:MM:SS

You can use the Time#strftime method to format the time into a variety of different formats, including the one which you need. It takes a parameter which specifies the format of the date output. Look at the documentation for this method for more instructions about how to use it.

Time.now.strftime("%F %T")

The %F specifies that we would like the date in YYYY-MM-DD format. This is followed by a space, then the %T specifier, which produces a HH:MM:SS time output.

To incorporate this into your code:

"erstellzeit": "#{Time.now.strftime("%F %T")}"

Alternatively, if you're getting an additional +0000 output, try:

"erstellzeit": "#{Time.now.strftime("%F %H:%M:%S")}"

Rails datetime format dd/mm/yyyy

For reading you can use strptime and specify the format:

datetime = "14/04/17 07:00"
DateTime.strptime(datetime, "%d/%m/%y %R")
=> Wed, 14 Apr 0017 07:00:00 +0000

Explanation

%d - Day of the month, zero-padded (01..31)
%m - Month of the year, zero-padded (01..12)
%y - year % 100 (00..99)
%R - 24-hour time (%H:%M)

For getting the date you can transform DateTime objects to Date objects using to_date or use .strftime("%d/%m/%Y") directly on DateTime to get String.

[47] pry(main)> a
=> Fri, 14 Apr 2017 07:00:00 +0000
[48] pry(main)> a.to_date
=> Fri, 14 Apr 2017
[49] pry(main)> a.strftime("%d/%m/%Y")
=> "14/04/2017"
[50] pry(main)> a.strftime("%R")
=> "07:00"

Full docs here. Also a full list of format directives is available on strftime docs

Parsing date using strftime

Here's some code that might help:

require 'date'
require 'time'

date = Date.strptime('2015-04-28 19:30:00 UTC', "%Y-%m-%d %H:%M:%S %Z")
# => #<Date: 2015-04-28 ((2457141j,0s,0n),+0s,2299161j)>
date.strftime('%m/%d/%y')
# => "04/28/15"

Parsing into a Date object stores only the year, month and day information.

datetime = DateTime.strptime('2015-04-28 19:30:00 UTC', "%Y-%m-%d %H:%M:%S %Z")
# => #<DateTime: 2015-04-28T19:30:00+00:00 ((2457141j,70200s,0n),+0s,2299161j)>
datetime.strftime('%m/%d/%y')
# => "04/28/15"

time = Time.strptime('2015-04-28 19:30:00 UTC', "%Y-%m-%d %H:%M:%S %Z")
# => 2015-04-28 19:30:00 UTC
time.strftime('%m/%d/%y')
# => "04/28/15"

Parsing into a DateTime, or Time object captures all the information.

The error you're seeing is often caused by a date string in a format you don't expect. That often happens when you expect a value in 'mm/dd/yy' order, but actually receive one in 'dd/mm/yy' order. This happens because the US uses 'mm/dd/yy' and most of the rest of the world uses 'dd/mm/yy':

date = Date.strptime('2015-28-04', '%Y-%m-%d')
# ~> -:7:in `strptime': invalid date (ArgumentError)

or

date = Date.strptime('28-04-2015', '%m-%d-%Y')
# ~> -:7:in `strptime': invalid date (ArgumentError)

You can work around that by using some rescue statements to try several different formats.

You don't want to use parse because it'll assume 'dd/mm/yyyy' and will blow up with US dates, and is slower because it tries multiple formats before giving up. So, using fixed formats you expect is the way to go but you need to search for your various formats of date/datetime strings you'll expect to find and write format strings to match. And, since you're scraping pages, it's possible to find a LOT of bad/malformed values so program defensively.

Ruby Datetime parse

Since you're supplying a two digit year, it should be:

Date.strptime(myDate, "%m/%d/%y") # Note the lower case y

If the time is also important, you should use:

DateTime.strptime(myDate, "%m/%d/%y %H:%M")


Related Topics



Leave a reply



Submit