The time stamp is missing on the parsing. What needs to be done to ensure that the timestamp is also there?
irb(main):060:0> dt= Date.parse "2021-08-06T15:00:00-04:00"
=> #<Date: 2021-08-06 ((2459433j,0s,0n),+0s,2299161j)>
irb(main):061:0> dt.strftime("%Y-%m-%d %H:%M:%S.%L")
=> "2021-08-06 00:00:00.000"
Consider using DateTime:
require 'date'
dt = DateTime.parse("2021-08-06T15:00:00+04:00")
result = dt.strftime("%Y-%m-%d %H:%M:%S.%L")
puts result
Yielding 2021-08-06 15:00:00.000.
Related
I know this sounds like a repeated question but I think this situation is different. I will delete this post if it truly is.
I have a string containing a date in the following format: Thu, Jun. 20
I would like to parse this into a Date variable and then increment it to the next day.
So far I have done
text = "Thu, Jun. 20"
date = Date.new
date = Date.strptime(text, '{%a, %m, %d}')
But this gives me the following error:
invalid date (ArgumentError)
I got this idea from: Ruby: convert string to date
All answers I have seen so far have been parsing strings that contain full information (the full month or day of the week). Is what I'm trying to do even possible ? If not any suggestions on a work around would be most appreciated.
You used wrong date format. After parse it, you can use plus or minus operator to change date.
Reference: http://www.ruby-doc.org/stdlib-1.9.3/libdoc/date/rdoc/DateTime.html#method-i-strftime
%a - The abbreviated name (``Sun'')
%b - The abbreviated month name (``Jan'')
%d - Day of the month, zero-padded (01..31)
Code:
1.9.3p392 :003 > require 'date'
=> true
1.9.3p392 :008 > date = Date.strptime(text, '%a, %b. %d')
=> #<Date: 2013-06-20 ((2456464j,0s,0n),+0s,2299161j)>
1.9.3p392 :009 > date + 1
=> #<Date: 2013-06-21 ((2456465j,0s,0n),+0s,2299161j)>
1.9.3p392 :010 > date - 1
=> #<Date: 2013-06-19 ((2456463j,0s,0n),+0s,2299161j)>
To answer your need I would like to parse this into a Date variable and then increment it to the next day. I tried below :
require 'date'
d = Date.parse("Thu, Jun. 20")
# => #<Date: 2013-06-20 ((2456464j,0s,0n),+0s,2299161j)>
d.to_s # => "2013-06-20"
d.next.to_s # => "2013-06-21"
Wondering what happened with these inputs that gave me something strange instead of 2013-05-31 13:30:00 -0400
Time.parse("05-31 13:30") => 2013-06-06 16:30:00 -0400
Time.parse("5 31 13:30") => 2013-07-01 13:30:00 -0400
#SergioTulentsev's comment points to the problem. Date.parse can't know every possible combination of ways people might want to structure a date/datetime value. That's why Date supports strptime, which lets YOU define the pattern:
require 'date'
DateTime.strptime("05-31 13:30", '%m-%d %H:%M')
=> #<DateTime: 2013-05-31T13:30:00+00:00 ((2456444j,48600s,0n),+0s,2299161j)>
DateTime.strptime("5 31 13:30", '%m %d %H:%M')
=> #<DateTime: 2013-05-31T13:30:00+00:00 ((2456444j,48600s,0n),+0s,2299161j)>
The problem then becomes one of which format string to use for a given date string. In this test I changed the second datetime string so it's more obvious that the code is working correctly:
require 'date'
DATE_PATTERNS = {
/[0-2]\d-\d{2} \d{2}:\d{2}/ => '%m-%d %H:%M',
/[0-2]?\d \d{1,2} \d{2}:\d{2}/ => '%m %d %H:%M'
}
puts ["05-31 13:30", "5 31 13:31"].map { |str|
pattern = DATE_PATTERNS.keys.find { |k|
str[k]
}
puts pattern.source
DateTime.strptime(str[pattern], DATE_PATTERNS[pattern]).to_s
}
Which outputs:
[0-2]\d-\d{2} \d{2}:\d{2}
[0-2]?\d \d{1,2} \d{2}:\d{2}
2013-05-31T13:30:00+00:00
2013-05-31T13:31:00+00:00
In your time-string the year is missing.
parse tries to fix this but fails.
With the year given - like so
Time.parse("2013-05-31 13:30")
parse works.
I have a date string 20101129220021, so I will use
require 'date'
d = DateTime.parse('20101129220021')
This part works fine, and I get a date, which is in UTC.
My question is, how can I convert this into my local time? I tried many methods like extracting the time part using d.to_time and manipulate the result, but it didn't work. As far as I know, DateTime object is immutable. Can I please get some help?
irb(main):001:0> require "date"
=> true
irb(main):002:0> d = DateTime.parse('20101129220021')
=> #<DateTime: 2010-11-29T22:00:21+00:00 (70719276007/28800,0/1,2299161)>
irb(main):003:0> d.to_time
=> 2010-11-30 00:00:21 +0200
ruby 1.9.2p180 (2011-02-18)
You can add a rational fraction based on the timezone to get the local time.
require 'date'
# Make this whatever your zone is. Using UTC +0300 here.
ZONE = 3
d = DateTime.parse('20101129220021') + Rational(ZONE,24)
d.to_s # => "2010-11-30T01:00:21+00:00"
I have a Time object and would like to find the next/previous month. Adding subtracting days does not work as the days per month vary.
time = Time.parse('21-12-2008 10:51 UTC')
next_month = time + 31 * 24 * 60 * 60
Incrementing the month also falls down as one would have to take care of the rolling
time = Time.parse('21-12-2008 10:51 UTC')
next_month = Time.utc(time.year, time.month+1)
time = Time.parse('01-12-2008 10:51 UTC')
previous_month = Time.utc(time.year, time.month-1)
The only thing I found working was
time = Time.parse('21-12-2008 10:51 UTC')
d = Date.new(time.year, time.month, time.day)
d >>= 1
next_month = Time.utc(d.year, d.month, d.day, time.hour, time.min, time.sec, time.usec)
Is there a more elegant way of doing this that I am not seeing?
How would you do it?
Ruby on Rails
Note: This only works in Rails (Thanks Steve!) but I'm keeping it here in case others are using Rails and wish to use these more intuitive methods.
Super simple - thank you Ruby on Rails!
Time.now + 1.month
Time.now - 1.month
Or, another option if it's in relation to the current time (Rails 3+ only).
1.month.from_now
1.month.ago
Personally I prefer using:
Time.now.beginning_of_month - 1.day # previous month
Time.now.end_of_month + 1.day # next month
It always works and is independent from the number of days in a month.
Find more info in this API doc
you can use standard class DateTime
require 'date'
dt = Time.new().to_datetime
=> #<DateTime: 2010-04-23T22:31:39+03:00 (424277622199937/172800000,1/8,2299161)>
dt2 = dt >> 1
=> #<DateTime: 2010-05-23T22:31:39+03:00 (424282806199937/172800000,1/8,2299161)>
t = dt2.to_time
=> 2010-05-23 22:31:39 +0200
There are no built-in methods on Time to do what you want in Ruby. I suggest you write methods to do this work in a module and extend the Time class to make their use simple in the rest of your code.
You can use DateTime, but the methods (<< and >>) are not named in a way that makes their purpose obvious to someone that hasn't used them before.
If you do not want to load and rely on additional libraries you can use something like:
module MonthRotator
def current_month
self.month
end
def month_away
new_month, new_year = current_month == 12 ? [1, year+1] : [(current_month + 1), year]
Time.local(new_year, new_month, day, hour, sec)
end
def month_ago
new_month, new_year = current_month == 1 ? [12, year-1] : [(current_month - 1), year]
Time.local(new_year, new_month, day, hour, sec)
end
end
class Time
include MonthRotator
end
require 'minitest/autorun'
class MonthRotatorTest < MiniTest::Unit::TestCase
describe "A month rotator Time extension" do
it 'should return a next month' do
next_month_date = Time.local(2010, 12).month_away
assert_equal next_month_date.month, 1
assert_equal next_month_date.year, 2011
end
it 'should return previous month' do
previous_month_date = Time.local(2011, 1).month_ago
assert_equal previous_month_date.month, 12
assert_equal previous_month_date.year, 2010
end
end
end
below it works
previous month:
Time.now.months_since(-1)
next month:
Time.now.months_since(1)
I just want to add my plain ruby solution for completeness
replace the format in strftime to desired output
DateTime.now.prev_month.strftime("%Y-%m-%d")
DateTime.now.next_month.strftime("%Y-%m-%d")
You can get the previous month info by this code
require 'time'
time = Time.parse('2021-09-29 12:31 UTC')
time.prev_month.strftime("%b %Y")
You can try convert to datetime.
Time gives you current date, and DateTime allows you to operate with.
Look at this:
irb(main):041:0> Time.new.strftime("%d/%m/%Y")
=> "21/05/2015"
irb(main):040:0> Time.new.to_datetime.prev_month.strftime("%d/%m/%Y")
=> "21/04/2015"
Here is a solution on plain ruby without RoR, works on old ruby versions.
t=Time.local(2000,"jan",1,20,15,1,0);
curmon=t.mon;
prevmon=(Time.local(t.year,t.mon,1,0,0,0,0)-1).mon ;
puts "#{curmon} #{prevmon}"
Some of the solutions assume rails. But, in pure ruby you can do the following
require 'date'
d = Date.now
last_month = d<<1
last_month.strftime("%Y-%m-%d")
Im using the ActiveSupport::TimeZone for this example, but just in case you are using Rails or ActiveSupport it might come in handy.
If you want the previous month you can substract 1 month
time = Time.zone.parse('21-12-2008 10:51 UTC')
time.ago(1.month)
$ irb
irb(main):001:0> time = Time.now
=> 2016-11-21 10:16:31 -0800
irb(main):002:0> year = time.year
=> 2016
irb(main):003:0> month = time.month
=> 11
irb(main):004:0> last_month = month - 1
=> 10
irb(main):005:0> puts time
2016-11-21 10:16:31 -0800
=> nil
irb(main):006:0> puts year
2016
=> nil
irb(main):007:0> puts month
11
=> nil
irb(main):008:0> puts last_month
10
=> nil
I have a Database column with the syntax "0000-00-00 00:00:00".
In PHP I would do
date('Y-m-d H:i:s');
In Ruby, I do
require 'date'
now = DateTime::now()
puts "#{now.year()}-#{now.mon()}-#{now.mday()} #{now.hour()}:#{now.min()}:#{now.sec()}"
The result is:
"2010-1-5 10:16:4"
That's not okay. How could I create a "timestring" with the format "0000-00-00 00:00:00"?
Thanks a lot in advance & Best Regards
You can format the dates like in PHP thanks to the built-in Time class, see documentation there.
This would give for your case :
t = Time.now
puts t.strftime("%Y-%m-%d %H:%M:%S")
A little shorter
t = Time.now
puts t.strftime("%F %T")
=> "2015-01-06 14:01:05"
iso8601 method can be useful also:
>> Time.now.utc.iso8601
=> "2015-05-08T16:45:22Z"
If you're using Rails/ActiveSupport then to_s(:db) will be the shortest way:
Time.now.to_s(:db)
=> "2017-05-22 11:14:40"
ActiveRecord tends to store dates / times as UTC, so using utc will make your data more consistant
t = Time.now.utc
puts t.strftime("%F %T")
=> "2016-05-06 19:05:01"
Note that Rails also stores microsecond precision. That is, microseconds are the 6 digits after the second in the database format:
Time.now.strftime('%Y-%m-%d %H:%M:%S.%6N')
=> "2022-10-26 12:30:26.243506"
or:
Time.now.strftime('%F %T.%6N')
=> "2022-10-26 12:34:37.803091"