Is There an Add_Days in Ruby Datetime

Is there an add_days in ruby datetime?

The Date class provides a + operator that does just that.

>> d = Date.today
=> #<Date: 4910149/2,0,2299161>
>> d.to_s
=> "2009-08-31"
>> (d+3).to_s
=> "2009-09-03"
>>

How to add 10 days to current time in Rails

Use

Time.now + 10.days

or even

10.days.from_now

Both definitely work. Are you sure you're in Rails and not just Ruby?

If you definitely are in Rails, where are you trying to run this from? Note that Active Support has to be loaded.

How do I add two weeks to Time.now?

You don't have such nice helpers in plain Ruby. You can add seconds:

Time.now + (2*7*24*60*60)

But, fortunately, there are many date helper libraries out there (or build your own ;) )

Time manipulation in ruby

A Time is a number of seconds since an epoch whereas a DateTime is a number of days since an epoch which is why adding 1 to a DateTime adds a whole day. You can however add fractions of a day, for example

d = DateTime.now
d + Rational(10, 86400)

Will add 10 seconds to d (since there are 86400 seconds in a day).

If you are using Rails, Active Support adds some helper methods and you can do

d + 20.minutes + 10.seconds

Which will do the right thing is d is a DateTime or a Time. You can use Active Support on its own, and these days you can pull in just the bits you need. I seem to recall that this stuff is in activesupport/duration. I believe there are a few other gems that offer help with time handling too.

how do I add N days to time T (accounting for Daylight Savings Time)?

ActiveSupport::TimeWithZone seems to handle this well

> t1 = ActiveSupport::TimeZone['Eastern Time (US & Canada)'].parse('2013-03-10')
=> Sun, 10 Mar 2013 00:00:00 EST -05:00

Notice the class type below:

 > t1.class
=> ActiveSupport::TimeWithZone

Notice the change from EST above to EDT below:

> t1 + 1.day
=> Mon, 11 Mar 2013 00:00:00 EDT -04:00

Ruby Date Subtraction (e.g. 90 days Ago)

require 'date'
now = Date.today
ninety_days_ago = (now - 90)

Running this thru the IRB console I get:

>>require 'date'
now = Date.today
ninety_days_ago = (now - 90)

require 'date'
=> false
now = Date.today
=> #<Date: 2011-03-02 (4911245/2,0,2299161)>
ninety_days_ago = (now - 90)
=> #<Date: 2010-12-02 (4911065/2,0,2299161)>


If you need the time you could just say now = DateTime.now

How can I find the number of days between two Date objects in Ruby?

Subtract the beginning date from the end date:

endDate - beginDate 

How to get the date by day of the week ruby

To get the current day in Rails, you can use:

Date.today

To get the previous Saturday (since today is Monday 3rd July, that's Saturday 1st July), you can use:

Date.today.beginning_of_week(:saturday)

Or, if what you actually wanted was the previous week's Saturday (Saturday 24th June), then you can use:

Date.today.weeks_ago(1).beginning_of_week(:saturday)

Or, if you prefer:

1.week.ago.beginning_of_week(:saturday)

...However, note that the above will return an ActiveSupport::TimeWithZone object rather than a Date object - so will behave slightly differently.

Have a read through the rails documentation - in particular, the ActiveSupport extensions to ruby's Date class to see what methods are available.

If, for some reason, you needed to do this in pure ruby (i.e. without the above mentioned ActiveSupport extensions that come bundled with rails), then you could instead utilise the Date.parse method:

require 'date'

Date.parse("Saturday") - 7
# => Sat, 01 Jul 2017

In order to convert your Date (or similar) object to the string format you desire ("24-June-2017"), you can use ruby's strftime method:

(Date.parse("Saturday") - 7).strftime('%d-%B-%Y')

In rails, it is a common convention to place such custom date formats in a locale or initializer to avoid repetition. You can then reference this format by name:

# config/initializers/date_formats.rb
# (Call this whatever you want!)
Date::DATE_FORMATS[:calendar] = '%d-%B-%Y'

# Anywhere else in the rails app
Date.today.beginning_of_week(:saturday).to_s(:calendar)
# => "01-July-2017"

How can I display the days of the week?

You can use a range and iterate through:

<% (Date.today.beginning_of_week(:sunday)...Date.today.end_of_week).each do |date| %>
<p><%= date.strftime('%a %d') %></p>
<% end %>

Will give you:

<p>Sun 28</p>
<p>Mon 29</p>
<p>Tue 30</p>
<p>Wed 01</p>
<p>Thu 02</p>
<p>Fri 03</p>
<p>Sat 04</p>

Note that we're using ... instead of .. for the range.



Related Topics



Leave a reply



Submit