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
Related
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:
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))
I have events (from an Event model) that have a starts_at: value in the form of a datetime. e.g.:
2016-02-18 11:00:00:00000
What I want to be able to do is check whether an event is starting this week.
I want to be able to make a list of events that are occuring this week (starting from the latest Monday).
#events = #calendar.events.where( ... )
I thought something along the lines of this:
start_week = Date.today.beginning_of_week(:monday).day()
end_week = Date.today.beginning_of_week(:monday).day()+6
range = start_week..end_week
#events = #calendar.events.where(starts_at: in range)
But it doesn't take into account the month or year. Also I'm not sure how to write the 'where' clause. How should I go about doing this? Thanks
Try this:
start_week = Date.today.beginning_of_week(:monday)
end_week = Date.today.beginning_of_week(:monday)+6
range = start_week..end_week
#events = #calendar.events.where(starts_at: range)
Assuming you want all the events from the current week, something like this should work:
#events = #calendar.events.where(starts_at: Time.zone.today.all_week)
all_week returns a Date range covering the current week.
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]
A number of holidays move around from year to year. For example, in Canada Victoria day (aka the May two-four weekend) is the Monday before May 25th, or Thanksgiving is the 2nd Monday of October (in Canada).
I've been using variations on this Linq query to get the date of a holiday for a given year:
var year = 2011;
var month = 10;
var dow = DayOfWeek.Monday;
var instance = 2;
var day = (from d in Enumerable.Range(1,DateTime.DaysInMonth(year,month))
let sample = new DateTime(year,month,d)
where sample.DayOfWeek == dow
select sample).Skip(instance-1).Take(1);
While this works, and is easy enough to understand, I can imagine there is a more elegant way of making this calculation versus this brute force approach.
Of course this doesn't touch on holidays such as Easter and the many other lunar based dates.
And it gets complicated if you have to consider non-christian holidays. For example, jewish holidays are based on the jewish calender..
Maybe not so elegant, but more error prone - you can just add a table of all relevant holidays for the next 100 years.
int margin = (int)dow - (int)new DateTime(year, month, 1).DayOfWeek;
if (margin < 0) margin = 7 + margin;
int dayOfMonth = margin + 7*(instance - 1) //this is for 0-based day number, add 1 if you need 1-based. instance is considered to be 1-based
Please note that I wrote it without trying to compile, so it is to give the idea, but may require some clean up. Hope this helps!