List Array of Days Between Two Dates - ruby

I would like to list an array of days between two dates. I can list an array of months with the code below. How might I change this to show every day between two dates?
require 'date'
date_from = Date.parse('2011-05-14')
date_to = Date.parse('2011-05-30')
date_range = date_from..date_to
date_months = date_range.map {|d| Date.new(d.year, d.month, 1) }.uniq
date_months.map {|d| d.strftime "%d/%m/%Y" }
puts date_months

I don't know which day you meant, thus I have shown all the ways.
#wday is the day of week (0-6, Sunday is zero).
(date_from..date_to).map(&:wday)
#mday is the day of the month (1-31).
(date_from..date_to).map(&:mday)
#yday is the day of the year (1-366).
(date_from..date_to).map(&:yday)
OP's actual need was not much clear to me. After few comments between us, I came to know from OP's comment, the below answer OP is looking for -
(date_from..date_to).map(&:to_s)

Related

Can't get monthy CarbonPeriod to behave as desired

In the project I'm working on I have a daily command that basically checks the date of the last record in the database and tries to fetch data from an API from the day after and then each month after that (the data is published monthly).
Basically, the last record's date is 2019-08-30. I'm mocking as if I were running the task on 2019-09-01 with
$test = Carbon::create(2019,9,1,4);
Carbon::setTestNow($test);
I then create a monthly period between the next day of the last record's date and the last day of the current month like so:
$period = CarbonPeriod::create($last_record_date->addDay(), '1 month', $last_day_of_current_month);
Successfully generating a period with start_date = 2019-08-31 and end_date = 2019-09-30. Which I use in a simple foreach.
What I expected to happen is that it runs twice, once for August and once for September, but it's running only once for the start date. It's probably adding a month and going past the end date, but I don't know how to force the behaviour I'm looking for.
TL;DR:
$period = CarbonPeriod::create('2019-08-31', '1 month', '2019-09-30');
foreach ($period as $dt) {
echo $dt->format("Y-m") . "<br>\n";
}
This will print just 2019-08, while I expect 2019-08 and 2019-09. What's the best way to achieve that?
Solution :-
You can store actual date in $actual_day and current date for occurring monthly in $current_day. Put a check on comparing both dates, if not matched then make it on the same day it will skip 30,31 case in case of February month.
$current_date = $current_date->addMonths(1);
if($current_date->day != $actual_day){
$date = Carbon::parse($date->year."-".$date->month."-".$actual_day);
}
Your start date is 2019-08-31. Adding a month takes you to 2019-09-31. 2019-09-31 doesn't exist so instead you get 2019-10-01, which is after your end date. To avoid this I'd suggest you use a more regular interval such as 30 days.
Otherwise you're going to have to rigorously define what you mean by "a month later". If the start date is 31st Jan is the next date 28th February? Is the month after 28th or 31st March? How do leap years affect things?

How to find the date number given the Year, month, day of the week and the week it falls on in ruby

So i'm new to ruby and not to familiar with the syntax of the Date gem, but is there a way to find the date of a moving holiday like fathers day?
So my current class for the moving holidays looks like this:
class MovingHoliday < Holiday
def initialize (name, month, day_of_week, week, year=2016)
#name = name
#month = month
#day_of_week = day_of_week
#week = week
#year = year
#my_date = Date.new(year, month, day)
end
end
and my input looks like this:
fathers_day = MovingHoliday.new("Fathers Day", 6, "sunday", 4, year)
The 6 is the month, "sunday" is well the day_of_the_week, and 4 is the week it falls on.
I just can't figure out the syntax if there is any that would/could do conversion for this.
And I can't use any gems other then rubys Date.
class MovingHoliday < Holiday
def initialize (name, month, day_of_week, week, year=2016)
#name = name
#month = month
#day_of_week = day_of_week
#week = week
#year = year
#my_date = Date.new(year, month, 1)
#my_date += 1 while !my_date.send("#{day_of_week}?")
#my_date += (7 * (week - 1))
end
end
Not pretty, but it works. It takes the first of the month, moves up a day until it's the correct day of the week, and then increases by the appropriate number of weeks.
As it stands, it assumes that you're given an actual day of the week. You'll probably want to sanitize the input in some manner or convert from "sunday" to 0 (my_date.wday returns 0 for sunday, 1 for monday, etc)
date in Ruby is not a gem, it's part of the standard library, it's just not loaded unless you require it. (In Ruby, require is used for both purposes, loading gems and loading parts of the standard library that are not loaded by default.)
I don't know of any built-in functionality that would calculate the date for you, I think you'd have to write that yourself, since you don't want to consider other gems.

VBScript DateDiff month

I am having a problem about getting the month datedifference between two dates.
Here is a sample:
DateDiff("m","2014-10-17","2014-10-30")
The above code returns 0 months since it is less than a month. But,
DateDiff("m","2014-10-17","2014-11-01")
returns 1 which should not be since it is still 15 days.
My problem is I want to see if these two dates already exceed a month but it seems that it calculates 1 month only when the month part of the date is changed.
DateDiff calculates the span between two timestamps with the precision defined in the first parameter. From what you described in your question and comments you rather seem to be looking for something like this:
ds1 = "2014-10-17"
ds2 = "2014-10-30"
d1 = CDate(ds1)
d2 = CDate(ds2)
diff = DateDiff("m", d1, d2)
If diff > 0 And Day(d2) < Day(d1) Then diff = diff - 1
WScript.Echo diff & " full months have passed between " & ds1 & " and " & ds2 & "."
You can calculate day difference between two dates using 'DateDiff("d",date1,date2)'.
Then calculate number of full-month by dividing 30 days.
Following is an example.
monthDiff = int(DateDiff("d","2014-10-17","2014-11-01") / 30)
As you know, days of months differs by month. For example, November has 30 days and December has 31 days.
If you wish to use 30 days a month when first day is on November, and to use 31 days a month whe first day is on December, the code need a table of days of months and routine to handle month information.

Determine the amount of clicks required to reach a specific date using a JQuery calendar?

I have a jquery calendar for the start date of a project.
Using Watir (automated browser driver, a gem for ruby), I have a set date that I would like to enter in.
The calendar start date is always today's date, whatever that may be for the day it is used. I was wondering if there was a way that ruby can process what today's date is, and use the specified date provided by the user, to calculate the difference of months between them.
Here is an example of the Calendar plugin: http://jqueryui.com/datepicker/
example:
today's date is 30/10/2012, if there was a project that were to start on the 20/12/2012, that would be 2 months from now, so 2 clicks on the next month button.
Is there a way I could do this?
Here is how I approached a similar situation with JSdatepicker:
$today = Time.now.strftime("%e").gsub(" ", "") #one digit day of month without leading space
#browser.text_field(:id => /dateAvailable/).click
Watir::Wait.until(60) {#browser.div(:id => /dateAvailable_popup_cal/).td(:text => $today).exists?}
#browser.div(:id => /dateAvailable_popup_cal/).td(:text => $today).click
Set or grab the date.
Click the text_field that fires the JSDatePicker object
Wait until the calendar actually pops up
The current month is shown, so choose today's date number.
In your case, you also need to set the month. Whether prompting the user for this, or choosing "today", the theory is the same:
$month = Date::MONTHNAMES[Date.today.month] #etc
Pseudo-code making lots of assumptions (only future dates, month name shown on calendar as text, etc):
while !#jquerytablewindow.text.include?($month)
next_month_button.click
end
I don't see a specific advantage to my method versus counting each month, unless of course we add a month to the calendar one day and you still want your code to work!
You could do:
#End date converted to date object
specified_date = '20/12/2012'
end_date = Date.parse(specified_date)
#Start date (today - 30/10/2012)
today = Date.today
#Determine difference in months
number_of_months_up_to_today = (today.month + today.year * 12)
number_of_months_up_to_end = (end_date.month + end_date.year * 12)
clicks_required = number_of_months_up_to_end - number_of_months_up_to_today
#=> 2
Basically it is counting the number of months since the year 0 and then finding the difference.

How can I create DateTime object based on the day?

I want to create an Datetime object based on the number of day in the year.
This number is from the 365 days of the year (for example it can be: 123 or 23 or 344...)
How can I do that?
Thanks
Use the DateTime.ordinal method. Here's an example to get the 100th day of year 2011.
require 'date'
year, day = 2011, 100
DateTime.ordinal(year, day)
# #<DateTime: 2011-04-10T00:00:00+00:00 (4911323/2,0,2299161)>
If you want it as the number of days from now you should do the following:
time = Time.new + (60*60*24)*(numberOfDaysFromNow)
If you want it as the number of days from the start of the year you should do the following
time = Time.new(Time.now.year) + (60*60*24)*(dayOfTheYear-1)

Resources