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
Related
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.
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)
I am working in ruby, i ahave an object containing today's datetime from database. I only want time truncating the date. How can i get that?
Try DateTime#strftime.
DateTime.now.strftime("%H:%M")
# => "12:17"
#!/usr/bin/ruby -w
time1 = Time.now
puts "Current Time : " + time1.hour + ":" + time1.min + ":" + time1.sec
Taken from & credit to - This TutorialsPoint Post.
If you use it really often you can use the gem time_splitter to help you set some accessors of date, time, hour and minute for your datetime attributes. Hope it helps.
You can check the different Rails date formats from here : Rails Date Formats
Example :
time = Time.now
time.strftime("%d %B %Y")
# => 12 December 2016
I am new to ruby on rails and got stuck in dates. I used Time.now to get the current time. Now I want to find out the srart and end dates for the current week and current month. For instance:
If Time.now returns me -
2012-08-13 15:25:35 +0530
Then the start and end dates for the current week are:
2012-08-12 - 2012-08-18
Then the start and end dates for the current month are:
2012--08-01 - 2012-08-31
I am implementing the same using the code:
Time.now.wday
=> 1
Time.now - 1.day
=> 2012-08-12 15:31:44 +0530
Time.now + 5.day
=> 2012-08-19 15:32:38 +0530
and so on.. But I dont find it a convenient way to do this. Iam sure Ruby on Rails does hold a better solution. Can anyone suggest to figure out the same..
Instead of using Time.now
Use the following code to get your queries solved
d = Date.today
d.at_beginning_of_week
#Mon, 13 Aug 2012
d.at_beginning_of_week.strftime
#"2012-08-13"
d.at_beginning_of_week.strftime("%d")
#"13" -> To print the date.
Similarly you can find multiple methods in the Date Class
Check out the Date class:
http://api.rubyonrails.org/classes/Date.html
The methods beginning_of_* and end_of_* look like exactly what you're after. For the week, you can even pass in what day you consider the start of the week.
I am using the Ruby gem Impressionist. It gives you the ability to look at a count of page hits from a specific date to today date. I'm trying to get the hits from the past 7 days but spit out the number for each day.
#widget.impressionist_count(:start_date=>"2011-01-01") #specify start date only, end date = now
How would I do that? I want 7 days ago but for each day to give me the exact count for that day.
Try this:
#widget.impressionist_count(:start_date => 1.week.ago)
You can get 1 week ago date like this
t = Time.now
lastweek = t - 1.week
and then you can do like this
#widget.impressionist_count(:start_date=>lastweek.strftime('%Y-%m-%d'))
I guess you shold do this in controller, or you can pass just the last week date to the view.
This code works in Rails app cause it uses active_support.
I tried impressionist with mongoid but the filter function didn't work at all in my enviroment.
i hope it does in your app.