How to get date of particular week in Rails - ruby-on-rails

I am working on Rails application where I am trying to fetch date of particular week but week start from not 1 January of year but some fixed date.
Like, my week start from 8 july 2016 (08-07-2016) so now i want to fetch start date and end date of any week.
Means week 1 start date -> 08-07-2016 and end date -> 14-07-2016.
Now i want to fetch any week start date and end date but how? I already tried but got solution of year start date not like this.
Any one have idea?
Thanks in advance.

Ok friends,
I found my answer below
week = 3
number = (week-1) * 7
start_date = Date.new(2016, 7, 8) +number.to_i
d = start_date.to_s.split('-')
end_date = Date.new(d[0].to_i, d[1].to_i, d[2].to_i) + 6
end_date = end_date.strftime("%Y-%m-%d")
Let me know if any one have confusion in this answer.

There is no need to split the start date and create a new date object.
start_date_of_first_week = Date.new(2016, 7, 8)
week_number = 3
start_date = start_date_of_first_week + ( (week_number - 1) * 7 )
end_date = start_date + 6

Related

Add multiple number of months to specific date until date in future - Google Sheets

In Google Sheets I need calculate next date in future from TODAY based on start date adding specific number of months (or multiple of them). When I will open it everytime in future it will always calculate correct date.
Example:
Today is 2021-12-01
Start date = 2021-02-07, interval = 5 months => next date should be 2021-12-07
Start date = 2021-09-07, interval = 3 months => next date should be 2021-12-07
Start date = 2021-09-07, interval = 12 months => next date should be 2022-12-07
Start date = 2021-12-07, interval = 12 months => next date should be 2021-12-07
Idealy would be to have 3 cells: start date, interval, future date.
I have no idea how to do it using one "unversal" formula.
Please help.
=DATE(YEAR(A1),(MONTH(A1)+B1),DAY(A1))
Where:
A1 = Start date
B1 = Interval
C1 = Formula (Next date)

How to add a number to the date ruby

I have method:
def date_of_next()
date = Date.parse('Monday')
delta = date > Date.today ? 0 : 7
date + delta
return date.strftime('%Y-%w-%d'), (date + 7).strftime('%Y-%w-%d')
end
This method specifies the date of Monday this week.
In return, I also return the date of Monday next week, but it does not display the date that needs to be
if I run this method in the .rb file then it prints everything well
2019-01-28
2019-02-4
but if run this method in rails controller, there is a problem with the date
2019-01-28
2019-01-04
As you see, there is a problem with the date of the month.
I do not understand what the problem is, help
I think you're going a little heavy on your code for a method that returns Monday this Week and Monday next week.
def beginning_of_weeks
monday = Date.new.beginning_of_week
return monday, monday + 7.days
end
You can, of course, add the formatting function strftime('%Y-%m-%d') inside the method, though it would be better organized somewhere else.
Maybe you mixed date format?
def date_of_next()
date = Date.parse('Monday')
delta = date > Date.today ? 0 : 7
date + delta
return date.strftime('%Y-%m-%d'), (date + 7).strftime('%Y-%m-%d')
end

.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 do I get a Date from a week number?

I want to find all items created in a given week, and pass in a week number param. (created_at is a normal timestamp.)
Given a week number, what is the easiest way to find a date in that particular week?
(Any date in the week will do, as I will use beginning_of_week and end_of_week in the scope.)
You can get Date objects representing the beginning and end of your week using the commercial method:
week = 41;
wkBegin = Date.commercial(2010, week, 1)
wkEnd = Date.commercial(2010, week, 7)
Now do your find:
Item.find(:all, :conditions->:create_date=>wkBegin..wkEnd.end_of_day)
Assuming you mean "a given week number in the current year", you can do the following:
2.weeks.since(Time.gm(Time.now.year))
=> Fri Jan 15 00:00:00 UTC 2010
Substitute (week_number - 1) for the 1 in the above, and you'll get a date in the desired week.

Groovy/Grails date class - getting day of month

Currently I'm using the following code to get year, month, day of month, hour and minute in Groovy:
Date now = new Date()
Integer year = now.year + 1900
Integer month = now.month + 1
Integer day = now.getAt(Calendar.DAY_OF_MONTH) // inconsistent!
Integer hour = now.hours
Integer minute = now.minutes
// Code that uses year, month, day, hour and minute goes here
Using getAt(Calendar.DAY_OF_MONTH) for the day of the month seems a bit inconsistent in this context. Is there any shorter way to obtain the day of the month?
If you add the following to your code it should assign the day of the month to the day Integer:
Integer day = now.date
Here's a stand-alone example:
def now = Date.parse("yyyy-MM-dd", "2009-09-15")
assert 15 == now.date
Isn't all those lines are kind of garbage?
The following two lines do the job in a groovysh
date = new Date()
date.getAt(Calendar.DAY_OF_MONTH)
To use this at your real code and not in the console
def date = new Date()
def dayOfMonth = date.getAt(Calendar.DAY_OF_MONTH)
The way you are doing it is the only way. The java date object stores the month and day, but doesn't store any information such as how long the given month is or what day of the month your on. You need to use the Calendar class to find out a lot of this info.
You can get just the day of the month from a Date in Groovy like this:
​Date date = new Date()
int dayOfMonth = date[Calendar.DAY_OF_MONTH]
This version is fairly terse:
Calendar.instance.get(Calendar.DAY_OF_MONTH)

Resources