Rails issue with parsing date - ruby-on-rails

I have datetime strings that are outputted in the following format:
03/27/2014 07:52:47 PM
I'm using a gem called rufus-scheduler, which takes strings in a YYYY-mm-dd hh-MM-ss #### (#### is timezone offset). Are there any easy ways to convert the first string I had to a string in the format rufus-scheduler likes?
I know that one way I could do it would be to create a method that parsed the string so I could create a new datetime object from it, and then call a strftime in the format rufus likes, but I was wondering if there were any more efficient ways to go about solving my problem.

You can use DateTime::strptime to parse, and DateTime#strftime to format it again.
First require 'date' and then:
2.0.0-p451 :021 > DateTime.strptime("03/27/2014 07:52:47 PM", "%m/%d/%Y %H:%M:%S %p").strftime("%Y-%m-%d %H-%M-%S %z")
=> "2014-03-27 19-52-47 +0000"

Check out Time.strftime
Also, http://www.foragoodstrftime.com/ is an awesome tool for this.

Chronic (https://github.com/mojombo/chronic) is your friend
require 'chronic'
p Chronic.parse('03/27/2014 07:52:47 PM')

Related

Rails 4: m/d/yy will not successfully format

I am pretty familiar with strptime in Rails, but I cannot get this date to format. The date is coming from a long .csv file so it would be a pain to pre-format the dates differently.
date = "2/9/17"
Date.strptime(date, "%-m/%-d/%y")
ArgumentError: invalid date
From the strptime documentation:
strptime does not support specification of flags and width unlike strftime
So you need a slightly simplified pattern:
date = "2/9/17"
Date.strptime(date, "%m/%d/%y")
# => #<Date: 2017-02-09 ((2457794j,0s,0n),+0s,2299161j)>
Date.strptime(date, "%m/%d/%y")?

Ruby - I need to convert string into a timestamp

I need to convert a string that represents a date to a timestamp object in Ruby.
For example:
date_string = "18-Feb-2016 09:01:04"
convert to a timestamp like so
2016-02-18 14:01:04
I need to save this to a mysql database were the column is type timestamp.
I have researched this for most of the day and can not find a solution. I know you can use Time.parse but that includes timezone and DateTime.parse().to_time includes the timezone. Since it has to be a timestamp i can not use strftime method.
I need the time to be included because it will be used for calculation purposes.
Any help would be greatly appreciated.
Thank you
TL;DR
datetime = DateTime.parse("18-Feb-2016 09:01:04").to_s(:db)
returns
"2016-02-18 09:01:04"
Here's a quick explanation...
1. Convert your string to a Date object with DateTime.parse
You can use the .parse method from the Date or DateTime class in order to parse a string. The parse method will return a Date object like this:
$ DateTime.parse("18-Feb-2016 09:01:04")
$ => #<DateTime: 2016-02-18T09:01:04+00:00 ((2457437j,32464s,0n),+0s,2299161j)>
.parse is a method provided by Ruby.
2. Format the string with DateTime.parse.to_s
Ruby on Rails gives you access to the DateTime.to_formatted_s method to change the formatting of the Date object prior to storing it in your database.
To match the format that you specified:
$ datetime = DateTime.parse("18-Feb-2016 09:01:04").to_formatted_s
Note: to_s is aliased from to_formatted_s and to_formatted_s is a method provided by Rails, not Ruby.
Use to_datetime method in Rails.
"12-10-2015".to_datetime
=> Mon, 12 Oct 2015 10:36:00 +0000
http://apidock.com/rails/String/to_datetime
Edited to add precise answer.
You can use .to_time or .to_datetime, the .to_time returns the date and time with timezone but the .to_datetime returns full date with week name but it shows +0000 as timezone, you will see the difference in both the formats, see the following example.
# 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
I've interpreted the question to be that you wish to convert the string "18-Feb-2016 09:01:04" to the string "2016-02-18 14:01:04" (generalized to arbitrary date-time strings, of course).
Let:
str = "18-Feb-2016 09:01:04"
What you want is done in two steps. The first is to convert this string to a DateTime object, that is, an instance of the class DateTime. The second step is to construct the desired string from the DateTime object.
One way to create the DateTime object is to use the method DateTime::parse:
require 'date'
DateTime.parse(str)
#=> #<DateTime: 2016-02-18T09:01:04+00:00 ((2457437j,32464s,0n),+0s,2299161j)>
That works fine for the string format you gave, but can be problematic with other formats. For example:
DateTime.parse "4-5-16 09:01:04"
#=> #<DateTime: 2004-05-16T09:01:04+00:00 ((2453142j,32464s,0n),+0s,2299161j)>
As long as you know the format that will be used, it's generally better to use DateTime#strptime with the appropriate pattern comprised of format directives:
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)>
See DateTime#strftime for the format directives.
For the problem at hand:
dt = DateTime.strptime(str, "%d-%b-%Y %H:%M:%S")
#=> #<DateTime: 2016-02-18T09:01:04+00:00 ((2457437j,32464s,0n),+0s,2299161j)>
The second step is to construct the desired string with the above-referenced strftime method:
dt.strftime("%Y-%m-%d %H:%M:%S")
#=> "2016-02-18 09:01:04"

Reformat date Rails 4

Looking for the best way to convert a string like "01/16/2016" into a friendly date format that rails can handle.
I have a calendar in which users can select a from_date and a to_date.. based on those params, my search will then filter results that fit the time periods.
Unfortunately, rails cannot handle the current format its in. Not sure the best way to go about this. I could change the search form's javascript to display the date differently, but I feel this format is most user friendly.
thx!
You can use the standard ruby Date class:
some_date = Date.strptime('01/16/2016', '%m/%d/%Y')
some_date will be an instance of Date, which then you can handle in rails and reformat in any way you want using strftime.
As like taglia said you can use the strptime
Date.strptime('01/16/2016', '%m/%d/%Y')
You can change the calendar date format from dd/mm/yyyy to mm/dd/yyyy, then you can use
require 'date'
date = DateTime.parse("16/01/2016")
=> #<DateTime: 2016-01-16T00:00:00+00:00 ((2457404j,0s,0n),+0s,2299161j)>
date.strftime('%a %b %d %H:%M:%S %Z %Y')
=> "Sat Jan 16 00:00:00 +00:00 2016"
You can use Date instead of DateTime if you want only the date.

Convert 2 types of date strings to datetime objects

I need a reliable way to convert formatted date strings into datetime objects so I can save into the database. I'm importing data from an RSS feed and the date strings look like this 10/31/2012 11:59:00 PM, but every once in a while one will come through with no time data, like this: 9/24/2012. Is there a way to convert the string to a datetime object regardless if the time is present or not?
I was using Date.strptime() because Date.parse() wasn't being reliable enough for me. Here's what I was using, which will error out when it encounters the date string without the time.
Date.strptime(i.xpath('activedate').text, '%m/%d/%Y %I:%M:%S %p').to_datetime
What would be the best way to handle this?
I recommend chronic:
require 'chronic'
Chronic.parse '10/31/2012 11:59:00 PM'
#=> 2012-10-31 23:59:00 0800
Chronic.parse '9/24/2012'
#=> 2012-09-24 12:00:00 0800
I'd just loop through an array of the possible date formats and try parsing until one succeeds:
parsed_date = nil
['%m/%d/%Y %I:%M:%S %p', '%m/%d/%Y'].each do |format|
parsed_date ||= DateTime.strptime(i.xpath('activedate').text, format) rescue nil
end

Ruby Regex extract parameters

I have a date in this format
11/28/2011 2:39:00 PM
I cant seem to get a way to extract to each element out of this format (like take out day, month, year, hour, minute, second, AM/PM) separately to work with them. I need help here, so i can use this to rearrange the time manually and work with it how i want. I cant find a solution for this. (but im pretty sure i would need to use regex)
require 'date'
str = '11/28/2011 2:39:00 PM'
date = DateTime.strptime(str, '%m/%d/%Y %I:%M:%S %p')
puts date.year # etc
Though I'm sure regex isn't the best way to do it, here are some regex's that I tested on rubular.com!
Month:
(\d){2}(?=\/\d\d\/)
Day:
(\d){2}(?=\/\d\d\d\d)
Year:
(\d){4}(?=\s)
Hour:
(\d){1,2}(?=:\d\d:\d\d\s)
Minute:
(\d){2}(?=:\d\d\s)
Second:
(\d){2}(?=\s\w\w\b)
AM/PM:
\b[PA]M\b
I've found it easiest to use the Chronic gem to do this kind of parsing. Chronic handles these a lot better than the default DateTime stuff:
gem install chronic
And then:
require 'chronic'
date = Chronic.parse(my_date)
puts "year, month, day, hour: #{date.year}, #{date.month}, #{date.day}, #{date.hour}"
Etc.
You don't have to parse it with regular expression, you can simply convert into DateTime:
require 'date'
my_time = DateTime.parse('11/28/2011 2:39:00 PM')
puts my_time.day # and so on
You can use DateTime.strptime http://ruby-doc.org/stdlib-1.9.3/libdoc/date/rdoc/DateTime.html#method-c-strptime to create a DateTime object and use that.
You can use this naive regex:
/(\d+)/(\d+)/(\d+)\s(\d+):(\d+):(\d+)\s(\w+)/
If your format never changes, ever, at all...

Resources