Ruby - convert string to date - ruby

I have a string like "2011-06-02T23:59:59+05:30".
I want to convert it to date format and need to parse only the date, "2011-06-02".

For Ruby 1.9.2:
require 'date' # If not already required. If in Rails then you don't need this line).
puts DateTime.parse("2011-06-02T23:59:59+05:30").to_date.to_s

require 'date'
d = Date.parse("2011-06-02T23:59:59+05:30")
d.strftime("%F")

Simplies way is
require 'date'
date = "2011-06-02T23:59:59+05:30".gsub(/T.*/, '')
DateTime.parse(date)

Time.parse() should allow you to parse the whole date time. Then you can use time.strftime( string ) to format that as just a date in a string.
date = Time.parse("2011-06-02T23:59:59+05:30")
date_string = time.strftime("%y-%m-%d")
of
date_string = time.strftime("%F")
(see Ruby Doc for Time for more output string formats)
The above should work if you want a string; if you want a date object to handle then the ruby Date class can help you handle it but I belive that everything still needs to be done with Time objects; see Ruby Doc for Date for details of the Date class.
Hope that helps, let me know if I have headed off in the wrong direction with my answer.

Related

Convert String to Date Object Ruby

I'm trying to comvert a string in my logs to a date object.
My string is 2018-09-18 11:42:50,286000201 which is the format YYYY-MM-DD hh:mm:ss,nnnnnnnnn
I'm trying to convert in to an object using the time library in ruby. The function I am using is Time.strptime('2018-09-18 11:42:50,286000201, '%Y-%m-%d %H:%M:%S,%9N')
Ruby is giving me an invalid striptime format. Any help would be appreciated. Cheers.
remove the 9, %N expects 9 digits bt default
Time.strptime('2018-09-18 11:42:50,286000201, '%Y-%m-%d %H:%M:%S,%N')
doc: http://ruby-doc.org/stdlib-2.1.1/libdoc/time/rdoc/Time.html#method-c-strptime

Mock the date for Date.parse

I am doing some tests on date parsing. I was able to mock the date by doing as below:
my_str_date = '08022014at0600 +00:00'
my_new_date = DateTime.strptime(my_str_date, "%d%m%Yat%H%M %z").to_time
Time.stub(:now).and_return(my_new_date)
This is good enough most of the time, but when I want to get a date based on the day of the week, I do Date.parse('wednesday') for example, and unfortunately, it takes the current date.
I don't know how to find a global solution to simulate a specific date in the year that would work for Time.now and Date.parse. Any idea?
I though about Timecop library, but it does not seem to handle my case.
require 'time'
def get_weekday_date(name, ref = Date.today)
day = {"sun"=>0, "mon"=>1, "tue"=>2, "wed"=>3, "thu"=>4, "fri"=>5, "sat"=>6}[name[0, 3].downcase]
ref - ref.wday + day
end
date = get_weekday_date('wed', Date.parse("2010-10-10"))
puts date.strftime('%Y-%m-%d %A') ## 2010-10-13 Wednesday
I was not able to do a clear integration test on a feature that parse a String because Date.parse does not use Time.now as reference for the current time. So I decided to use Chronic.parse from chronic gem and not Date.parse anymore.
The mock on Time.now is now use when I call Chronic.parse('wednesday'), and it helps me to check my feature is working properly based on a specific Time.now

Generate Timestamp in Ruby

I am getting a timestamp value in an xml request as the following format.
2014-06-27T12:41:13.0000617Z
I need to form the xml response with this kind of time format in ruby. How do I get this format for the corresponding time?
I wanted to know the name of this format.
Try this:
t = Time.utc(2010,3,30, 5,43,"25.123456789".to_r)
t.iso8601(10)
This produces:
"2010-03-30T05:43:25.1234567890Z"
require 'date'
datetime = DateTime.parse('2014-06-27T12:41:13.0000617Z')
repr = datetime.strftime('%Y-%m-%dT%H:%M:%S.%7NZ')
puts repr
#=> 2014-06-27T12:41:13.0000617Z

Ruby: Transform a string of time to xsd:datetime conforming string?

I have this: "2013-02-23 18:06:00 UTC"
and need this: "2013-02-23T18:06:00Z"
to conform to this: http://books.xmlschemata.org/relaxng/ch19-77049.html
Does anyone know of a good library/tool/method in Ruby to do this without having to write some transformation method?
Check out the rubydocs for Datetime here. There's a method to convert into ISO8601.
Within Rails:
DateTime.now.utc.strftime()
=> "2012-08-22T17:55:12+00:00"

Datetime in ruby

How to i convert "04/16/2012 03:44:26" string to datetime object in ruby?
I knew that "20120416" can be converted to datetime object by DateTime.parse(srting).
Please some one help me in converting "04/16/2012 03:44:26" to datetime object in ruby. And i need to add x minute to the above datetime object.
You need to specify the format using DateTime#strptime:
DateTime.strptime("04/16/2012 03:44:26", '%m/%d/%Y %H:%M:%S')
See: http://ruby-doc.org/stdlib-1.8.7/libdoc/date/rdoc/DateTime.html#method-c-strptime
You can add x minutes like so: + (x / (24 * 60.0)).
If you are using Rails or have no problems using ActiveSupport, you can just do:
require 'active_support'
new_datetime = datetime + 10.minutes
(Time.parse("04/16/2012 03:44:26") + 10.minutes).to_datetime
UPDATE
Works even in Ruby 1.8.7, but you need to require ActiveSupport (used automatically in Rails).

Resources