Converting a month and year to a date - ruby-on-rails

In my Rails 4 app I receive a month and a year from the user which I would like to make into a date. The new date should be the 1st of the following month but I'm not sure on the best way to tackle this.
At the moment I have
expiry = Date.new(params[:expYear],params[:expMonth].to_i + 1,1)
but this isn't ideal because if someone entered 12 as the month my approach would make the month 13 which of course doesn't exist (and also the year wouldn't get updated either).
Are there any useful functions that could help here?

You can do this:
year,month = params[:expYear].to_i,params[:expMonth].to_i
Date.new(year,month) + 1.month
or use the end_of_month as #dax wrote:
Date.new(year,month).end_of_month + 1.day
That will resolve your problem

I think this would be more simple
expiry = Date.new(params[:expYear],params[:expMonth].to_i).next_month

I solved it using this - sorry just before your answer user2503775
expiry = Date.new(params[:expYear].to_i,params[:expMonth].to_i) + 1.month
Note You have to convert to integers before this can be done. I also found out that if the day param is left out it defaults to 1 anyway so that's handy

I would do something like
Date.new(params[:expYear].to_i,params[:expMonth].to_i,1).advance(months: 1)

Do something like
Time.zone.local(params[:expYear].to_i, params[:expMonth].to_i, 1, 0, 0, 0)

Related

Month Dependent Text in Rails

I have a bit of test that needs to only appear on my site during the months of Aug through Nov, and all other months the text should be something different. What is the best way to accomplish this?
I would think it would look something like the following, but I am not sure how to limit it to just the month, so that the switch will happen every year.
IF Date.today IS BETWEEN ???August AND ???Nov DO
...
ELSE
...
Use .month and check if it's value included into expected list of accepted months
accepted_months = Set.new([8, 9])
if accepted_months.include?(Time.current.month)
else
end

Rails timestamps, See how much time passed since last time

In my app I want to print out the duration of time I spent from I made the model until I updated it.
So if I have the value
:created_at set to 2016-04-13 14:00:49 UTC
and
:updated_at set to 2016-04-13 15:05:49 UTC
I want to print out that it took 1hour and 5minutes. (or just 01.05).
How do I do this?
I'm not sure why this was downvoted, though a little googling would probably have gotten you to the right answer fairly quickly.
What you're looking for is called time_ago_in_words
Here is the doc http://apidock.com/rails/ActionView/Helpers/DateHelper/time_ago_in_words
And usage is:
time_ago_in_words #object.created_at
time_ago_in_words #object.updated_at
If you want to use it the console to play with it, make sure you preface it with helper so it's loaded into console
e.g.
helper.time_ago_in_words #object.created_at
update
For checking between 2 dates, not just one date from right now, then you can use distance_of_time_in_words
distance_of_time_in_words(#object.created_at, #object.updated_at)
That gives words like
12 days ago
If you're ONLY looking for hours, and nothing else then you can use basic subtraction and division
#object.updated_at.to_i - #object.created_at.to_i) / 60 / 60
You can try this
diff =#object.updated_at.to_time.to_i - #object.created_at.to_time.to_i
hour = "#{diff / 3600}:#{(diff % 3600) / 60}"

.change function is not working for Dates for even number of months in ruby

Hi I have define this method
def change_date
date = Date.today
start_date = date.change(year: 2015, month: (2 * 3)).at_beginning_of_quarter
p 'aaaaaa'
p start_date
end
give me invalid date error .change is not working or am I doing it in a wrong way please guide me how to solve this. Thanx in advance.
This is because the month you are specifying doesn't have the current day.
I mean the current month (July) has 31 days but the month you're setting (June) has only 30 days. You can change your code like so:
# in Rails:
date = Date.today.beginning_of_month # or Date.today.change(day: 1)
Then chain your 'change' in front of the date variable.
This actually happens, because today is the 31 of July, and not all months have 31 days in it, for example June, the 6th month, has only 30 days in it.

How to get the last date of next month in Rails

I need to find out the last day of the next month.
I tried but couldn't find out.
Example: Date.today.end_of_month.next_month
Date.today.next_month.end_of_month
Only in Rails or with Active Support:
(Date.today + 1.month).end_of_month
or with time:
1.month.from_now.end_of_month
use
(Date.today + 1.month).end_of_month

ruby on rails 3.0 TimeWithZone add hours can't be coerced into Fixnum

I'm getting a really strange error in my code that I'm hoping someone can help me out with. I have a model which has a created_at column and a duration column I have created. The duration is simply an integer which is assumed to be a number of hours. I am then trying to compare the current time to the created_at time + the duration to see if the row has expired. My code looks like this:
#expire_time = purchase.duration.hours + purchase.created_at
if(#expire_time > Time.current)
#do some stuff if the current time isn't greater than the expiration time
end
When I run that first line in the console it works fine, but when I save my code and call it through the route its part of, I get the following error at that line.
ActiveSupport::TimeWithZone can't be coerced into Fixnum
Does anyone have any thoughts on what might be the issue/how I go about making this comparison properly?
I found the problem...
this does not work:
purchase.duration.hours + purchase.created_at
but this does:
purchase.created_at + purchase.duration.hours
You just saved my life (or at least 2 past hours...), thanks!
DateTime.current + 7.days
Works well, return date time one week later
7.days + DateTime.current
TypeError (DateTime can't be coerced into Integer)`

Resources