Finding start and end date of month - ruby

I found code example for finding the start and end day of the current month I'm in. I tried to jump start this code, but am having a hard time figuring out what I have gone wrong.
month = params[:month].to_i
date_start = DateTime.new Date.today.year, params[:month], 1
date_end = (date_start >> 1) + 1 # will add a month and a day
#trips = Trip.all :date => date_start..date_end
I'm not 100% sure what to feed into params. Hope someone can help.

I am not sure if I understand your question correctly, maybe what you need is this? :
month = 9
date_start = Date.new(Time.now.year, month, 1)
date_end = date_start.next_month - 1

params should contains month(numeric) i.e between 1 - 12
For e.g params = {:month => '4'}
Also in second line use month instead of params[:month]

Related

Ruby - How many days?

I need to calculate how many days lived on earth based on three parameters : day, month and year.
I don't know how to convert the input onto a date; and then how to convert the date onto number of days.
This is my code for the moment...
require 'date'
def age_in_days(day, month, year)
lived = (day+"-"+month+"-"+year).to_s
date_lived Date.parse(lived)
return (Date.today - date_lived).to_i
end
You can create a Date object directly with:
Date.new(year, month, day)
There's no need to convert it to a string and parse. Parsing could also be dangerous. Is 1982-04-02 the 4th of February or 2nd of April?
You cannot add a number and string like this, BTW. Should 1 + '2' be '12' or 3? Ruby cannot decide for you so you need to explicitly convert integers to string.
day.to_s + "-" + month.to_s + "-" + year.to_s
or simply
[day, month, year].join('-')
But you don't need it anyway:
require 'date'
def age_in_days(day, month, year)
birthdate = Date.new(year, month, day)
(Date.today - birthdate).to_i
end

Given a date, how to find if it's the second or fourth Saturday of the month

In my Rails application, I need to find if a given date is the second or fourth Saturday of the month. What's the efficient way to do this? Is there a gem I can use?
Days 1 to 7 are week 0
Days 8 to 14 are week 1
Days 15 to 21 are week 2
Days 22 to 28 are week 3
To get the week id, we can calculate (date.day-1)/7. Since the id is zero-based, the second and fourth saturdays have an odd week id :
def second_or_fourth_saturday?(date)
date.saturday? && ((date.day - 1) / 7).odd?
end
The second Saturday has to be in the day range 8-14, and the fourth in the day range 22-28. So I think this should work
def second_or_forth_saturday?(date)
return false unless date.saturday?
(8..14).include?(date.day) || (22..28).include?(date.day)
end
def fourth_saturday?(date)
saturdays = (date.beginning_of_month..date.end_of_month).select { |date| date.wday == 6 }
[satudays.second, saturdays.fourth].include?(date)
end
create a month dates range
select Saturdays
see, if second or fourth Saturday is equal to date
As #Stefan kindly suggested, the first line of the method could be written as follows (using all_month):
saturdays = date.all_month.select(&:saturday?)
References:

Get difference between two dates in days using ruby

I would like to know the time difference between two dates (Time/DateTime class) in days, i have one date created from the Date class and the other coming from my rails model, the one coming from the model is of the Time class, i want to know the number of days in between.
#current_day = Date.new
#created_day = establishment.created_at
i have tried getting this result using the days_ago function but it doesn't take into consideration the months.
old_date = Date.parse('2016-08-10')
new_date = Date.parse('2016-09-02')
days_between = (new_date - old_date).to_i
You have to convert days betweet to integer, because otherwise the result would be instance of Rational class.
Solution to your question after edit:
#current_day = Date.new
#created_day = establishment.created_at
days_between = (#current_day - #created_day.to_date).to_i
Solution to your question: Try this one it will work.
days_between = ("Tue Oct 24 09:20:25 UTC 2017".to_date.."Fri Oct 27 11:20:08 UTC 2017".to_date).count
If you print the result(days_between) you will get result as 4

VB6 week of day function

I'm relatively new to VB6 and I've just been given an assignment where I have a date - for example '4/12/2016' - from this date, i'm trying to find out the day that it is. So let's say it's a wednesday. Now from this day, I'm trying to determine the dates for the week [sun(startdate) - sat(enddate)). How would I go about doing something like this?
EDIT: I have a pretty good idea about finding out the date for sunday and saturday, since I can simply do something along the lines...
dim dateStart,dateend as date
Ex of date given to me = '4/12/2016'
Dim dateDay as variant
dateDay = whatever I get here - i'm assuming that a date will return a number for whatever day it is ???? Not sure
Select Case dateDay
case 1 -Monday?
dateStart=dateadd("d",-1,'4/12/2016)
dateEnd = dateadd("d",6, '4/12/2016)
case 2 -Tuesday?
datestart = dateadd("d",-2,'4/12/2016)
dateend = dateadd("d",5,'4/12/2016)
End Select
Basically do the SELECT statement for all cases. Am I on the right track?
This code:
Debug.Print Format(DatePart("w", Now), "dddd")
will print whatever day of the week it is now to the Immediate window. If you want the abbreviated day of week, use "ddd" for the format.
Now, this code:
Dim DOW As String
Select Case DatePart("w", Now)
Case vbSunday
DOW = "Sunday"
Case vbMonday
DOW = "Monday"
Case vbTuesday
DOW = "Tuesday"
Case vbWednesday
DOW = "Wednesday"
Case vbThursday
DOW = "Thursday"
Case vbFriday
DOW = "Friday"
Case vbSaturday
DOW = "Saturday"
End Select
Debug.Print DOW
will do the same thing. However, it shows you how to evaluate programmatically which day of the week you're dealing with, by using vbSunday, vbMonday, etc. That should give you what you need to get started on your Select statement. To use your example, DatePart("w", "4/12/2016") evaluates to 3, or vbTuesday.
VB6 reference documentation is here, and rather well hidden I might add. Look up Format and DatePart to get familiar with other options.
EDIT: As MarkL points out, the Weekday function is available in VB6 (I thought it wasn't), and is simpler (one less argument) than using DatePart. This code:
Debug.Print Format(Weekday(Now), "dddd")
will also print whatever day of the week it is to the immediate window. jac has also provided a link to the Weekday function in the comments above.
You can try below codes, The code will return name of the day.
txtDateTime.Text = WeekdayName(Weekday(Now))
txtDateTime.Text = WeekdayName(Weekday(12 / 30 / 1995))
txtDateTime.Text = WeekdayName(Weekday(Date))

Get what date it is based on week number and day number asp classic?

I need to know what date it is based on a week number and a day number.
So if I have weeknr=3 and day=1(for today- sunday) and lets say the year=2016 how can I make this into 2016-01-24.
Any input really appreciated, thanks.
Your best help will be the DatePart function (see here for docs). It's not altogether straightforward, because there are options to consider like "first day of week" and "first week of year", but you can define those in DatePart.
This would be my solution:
option explicit
function getDateByWeek(year, week, day)
dim dt, woy, add_days
dt = DateSerial(year, 1, 1)
do
woy = DatePart("ww", dt, vbSunday, vbFirstFullWeek)
' possibly change options [,firstdayofweek[,firstweekofyear]]
dt = DateAdd("d", 1, dt)
loop while woy<>1
add_days = week * 7 + day - 2
' -1 because we already added one day extra in the loop
' -1 to correct given day (sunday = 1)
getDateByWeek = DateAdd("d", add_days, dt)
end function
Response.Write "RESULT: " & getDateByWeek(2016, 3, 1) ' -> 24.01.2016
I start by finding the first day of the first week in a loop and then adding the cumulative amount of days to have a result.
Well the best way is to work out what the week start of the year is and take it from there.
Firstly, the first Thursday of any year is always in the first week of the year, so a simple calculation of adding the correct number onto the 1st January is obvious. After this we simply mulitply out the weeks and add the days:
Const C_THURSDAY = 5
Function GetDateFromYWD(y, w, d)
Dim tDate, aDate
'Get the first of the year...
tDate = DateSerial(y, 1, 1)
'Workout the start of the first week in the year...
aDate = tDate + Int(WeekDay(tDate)>C_THURSDAY And 7) - WeekDay(tDate)
'Add on the number of weeks plus days we're looking for...
aDate = aDate + (w * 7) + d
GetDateFromYWD = aDate
End Function

Resources