I am trying to create a method that takes user entered month and year and prints out an accurate calendar for the month and year. I am about halfway through but have no idea how to move on.
The guidelines Below in Bold is where i am having issues
Determine the beginning of the next month. Calculate the last day of the month by doing the following: Get the next month date, now get the previous day of that next month. Print the Calendar heading for the full month name and year. Print the header row that contains the abbreviated name of the days of the week. Create an array with a length of 7 and a default value of 4 spaces (" ").
Using the date range from the beginning of the month to the end of the month do the following:
Get the numerical representation of the day of the week using the" %w" formatter and convert to an integer. (This will be the index to the array.)
Use the index to set the day of the month within the array, using the "%d" formatter to get the day of the month.
If index equals 6 OR the date equals the last day of the month, then do the following:
Loop through the array
Print the day of the month
Print a blank line Re-initialize the array so that all values are of 4 spaces
def printMonth(year, month)
now= Date.new(year, month, 1)
a= now >> 1
b= a - 1
puts("Calendar for: #{b.strftime("%B, %Y")}")
puts("")
puts("Sun\tMon\tTue\tWed\tThu\tFri\tSat")
days= Array.new(7," ")
c= b.strftime("%w").to_i
end
Related
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?
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.
How Can I display Month Name Instead of Month Number (Not Data Format Number) In Crystal Report?
The MonthName function can be used to display the name of the month, when you provide a number between 1 and 12 (1 being January). It is useful for showing the month name in Group titles or labeling groups in charts.
It can be combined with the DatePart function to return the month name of a variable or calculation.
Syntax
MonthName(month, abbr)
Month A number from 1 to 12.
abbr Optional.A Boolean value. If true, the month name is abbreviated. The default is false.
Examples
Example Result
MonthName(5) “May”
MonthName(10) “October”
MonthName(10,True) “Oct”
MonthName(DatePart(“m”, CurrentDate)) “October” when the current date is 10/5/10.
I am stumped. I have a Timesheet class that holds work days in a dictionary called self.timesheet with dates as the keys and hours, rate as values. I am trying to write a function that can show all the entries in a user defined range of dates.
for now lets assume the key dates are simple integers 20 - 25. i tried this and it didn't do anything at all. no errors, just nothing.
def show_days(self):
date_from = input("From date: ")
date_to = input("To date: ")
t = self.timesheet
for dates in t[date]:
range(date_from, date to)
print(dates)
I can see this doesn't look right , I feel I need *for dates in range(date_from, date_to)* but I can't figure how to get it to loop over the dictionary keys like that.
You need to loop over the range, then check if that key is in the dictionary:
for day in range(date_from, date_to + 1):
if day in t:
print day, t[day]
Note that the values produced by range() do not include the end point, so I used date_to + 1 to ensure it is included anyway.
If your keys are not integers but, say, datetime.date objects, you'll have to construct some kind of loop with datetime.timedelta() to iterate over all dates between two values:
date = date_from = datetime.date(2012, 1, 15)
date_to = datetime.date(2012, 3, 12)
while date <= date_to:
if date in t:
print date, t[date]
date += datetime.timedelta(days=1)
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.