Page 1 of 1
Help to Calculate number of days in a month?
Posted: Fri Jan 08, 2010 3:08 am
by Skyfisher
Hi All, I'm brand new to Revolution, but am excited to use it. I'm evaluating it for possible use in my department at work. Anyway, I'm starting out by becoming familiar with the features and language.
I've done the starter project for the Calendar (I think it needs some updating) and now I'm adding my own features.
Can someone provide some guidiance on how a person might go about calculating the number of days in any given month starting from a known date, such as Jan 1st, 2010? Basically I'm trying to write code to have it automatically populate each month after you tell it the month you will start at. The provided functions don't seem to provide a way to calculate how days a month has, so I'll have to calculate those numbers with a custom function.
Any guidance is appreciated.
Re: Help to Calculate number of days in a month?
Posted: Fri Jan 08, 2010 4:13 am
by sturgis
Well with the exception of leap year, months have the same number of days every year.
jan 31
feb 28
march 31
april 30
may 31
june 30
july 31
august 31
sept 30
oct 31
nov 30
dec 31
So all you have to do is figure out if its a leap year so you can add a day to feb.
http://en.wikipedia.org/wiki/Leap_year
If you're using the standard gregorian calendar if the year mod 4 is 0, its a leap year.
You'll also need to be able to find what day of the week the month starts on, but that shouldn't be too difficult using the built in time/date functions.
Re: Help to Calculate number of days in a month?
Posted: Fri Jan 08, 2010 5:24 am
by Skyfisher
Sturgis, Thank you, I had not thought of it that way, that would make it easier using cases as you suggest.
I've been tyring to code it by doing something the the below:
Ask for the starting date of the calendar (make sure its the 1st day of a month)
Then convert it to dateitems
Then set the month day to the 28 (shortest possible month) using item 3 of dateitems
Add 1 to item 3 of dateitems
convert new dateitems to short date
Check to see if the month changed ( item 1 of short date)
If Yes then the number of days is the previous day stored from dateitems
If no Add 1 more to the current dateitems and check again
etc...
I can get the day of the week from item 7 of dateitems
Re: Help to Calculate number of days in a month?
Posted: Fri Jan 08, 2010 5:50 am
by sturgis
Or, since item 2 is the month number, and you know, with the exception of feb how many days are in the month, you can do something like this.
put "31,28,31,30,31,30,31,31,30,31,30,31" into tDaysOfMonth
get your start date and convert to dateitems at which point item 2 is the month number.
since they're in order, if the month number is 4, you can
Code: Select all
put item 4 of tDaysOfMonth into theNumDays
Or you could do the same with an array keyed by the number of the month.
so theDaysA[1] would contain 31 theDaysA[2] would contain 28 etc.
At which point you can put theDaysA[the month number you got from the dateitems] into theNumDays
and it will return the number of days for that month.
The only thing you have to catch is feb, so you can have something like
Code: Select all
if theNumDays = 28 and theYear mod 4 = 0 then
add 1 to theNumDays
end if
Re: Help to Calculate number of days in a month?
Posted: Fri Jan 08, 2010 4:19 pm
by bangkok
For a wonderful calendar :
http://www.troz.net/rev/stacks/Calendar.rev
And for a function to calculate the number of days in a month :
http://www.troz.net/rev/stacks/DateTime.rev
why bother to remake the circle ? Sarah has done it.

Re: Help to Calculate number of days in a month?
Posted: Fri Jan 08, 2010 5:57 pm
by mario baccheschi
Re: Help to Calculate number of days in a month?
Posted: Fri Jan 08, 2010 8:59 pm
by bn
On the list recently
http://n4.nabble.com/EASY-way-to-add-n- ... ml#a975894
Zryip TheSlug posted this neat handler to determine how many days a month has.
two handlers: one for english date and one for system date, i.e. the localized date format the system uses
A conversion function really handy!
An easy trick to know the end date of one month without creating
complex script to test the number of days in the months, years
leap, etc..
Take the first date of the month, then add one month and substract one
day, like this :
on mouseUp
put "12/01/2009" into lMyDate
answer addToDate(lMyDate,0,1,-1)
end mouseUp
function addToDate pStartDate,pAddToYear,pAddToMonth,pAddToDay
convert pStartDate to dateitems
add pAddToYear to item 1 of pStartDate
add pAddToMonth to item 2 of pStartDate
add pAddToDay to item 3 of pStartDate
convert pStartDate to short date
return pStartDate
end addToDate
-Zryip TheSlug wish you the best !
on mouseUp
local lMyDate
set useSystemDate to true -- Use the system settings instead of the RR natural english date
put the date into lMyDate
answer addToDate(firstDateInMonth(lMyDate),0,1,-1)
end mouseUp
function firstDateInMonth pStartDate
-- Return the first day in a month
set useSystemDate to true
convert pStartDate to dateitems
put 1 into item 3 of pStartDate
convert pStartDate to short date
return pStartDate
end firstDateInMonth
function addToDate pStartDate,pAddToYear,pAddToMonth,pAddToDay
-- Allows you to manipulate a date in a single pass
set useSystemDate to true
convert pStartDate to dateitems
add pAddToYear to item 1 of pStartDate
add pAddToMonth to item 2 of pStartDate
add pAddToDay to item 3 of pStartDate
convert pStartDate to short date
return pStartDate
end addToDate
zryip theSlug
regards
Bernd
Re: Help to Calculate number of days in a month?
Posted: Sat Jan 09, 2010 3:55 am
by Skyfisher
Wow, I never imagined so many options.
Sturgis, bankgkok, mario, and bernd, you guys are awesome! Thank you. I am studying your solutions, a lot of fun.
Thank you!
Re: Help to Calculate number of days in a month?
Posted: Sun Feb 12, 2023 12:56 pm
by richmond62
-
Largely rubbish as assumes that EVERY year has 365 days, and that EVERY February has 28 days.
But you should not expect much more for 10 minutes work.

Re: Help to Calculate number of days in a month?
Posted: Sun Feb 12, 2023 1:30 pm
by richmond62
If you're using the standard gregorian calendar if the year mod 4 is 0, its a leap year.
Not really, because if a year is divisible by 400 it is NOT a leap year.
-