Help to Calculate number of days in a month?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
Skyfisher
Posts: 15
Joined: Fri Jan 08, 2010 2:46 am

Help to Calculate number of days in a month?

Post by Skyfisher » Fri Jan 08, 2010 3:08 am

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.
Skyfisher
Round Rock, Texas
Mac OS

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Help to Calculate number of days in a month?

Post by sturgis » Fri Jan 08, 2010 4:13 am

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.

Skyfisher
Posts: 15
Joined: Fri Jan 08, 2010 2:46 am

Re: Help to Calculate number of days in a month?

Post by Skyfisher » Fri Jan 08, 2010 5:24 am

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
Skyfisher
Round Rock, Texas
Mac OS

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Help to Calculate number of days in a month?

Post by sturgis » Fri Jan 08, 2010 5:50 am

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

bangkok
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 937
Joined: Fri Aug 15, 2008 7:15 am

Re: Help to Calculate number of days in a month?

Post by bangkok » Fri Jan 08, 2010 4:19 pm

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.
;-)

mario baccheschi
Posts: 13
Joined: Mon Oct 26, 2009 7:30 pm

Re: Help to Calculate number of days in a month?

Post by mario baccheschi » Fri Jan 08, 2010 5:57 pm

a little help
from a translated HyperCard stack:

http://xoomer.virgilio.it/mbacches/KALENDA/test.html

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4171
Joined: Sun Jan 07, 2007 9:12 pm

Re: Help to Calculate number of days in a month?

Post by bn » Fri Jan 08, 2010 8:59 pm

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 ! 8)

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

Skyfisher
Posts: 15
Joined: Fri Jan 08, 2010 2:46 am

Re: Help to Calculate number of days in a month?

Post by Skyfisher » Sat Jan 09, 2010 3:55 am

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!
Skyfisher
Round Rock, Texas
Mac OS

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10096
Joined: Fri Feb 19, 2010 10:17 am

Re: Help to Calculate number of days in a month?

Post by richmond62 » Sun Feb 12, 2023 12:56 pm

SShot 2023-02-12 at 13.54.26.png
-
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. :D
Attachments
DATE GAPPER.livecode.zip
Stack.
(1.79 KiB) Downloaded 114 times

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10096
Joined: Fri Feb 19, 2010 10:17 am

Re: Help to Calculate number of days in a month?

Post by richmond62 » Sun Feb 12, 2023 1:30 pm

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.
-
SShot 2023-02-12 at 14.29.06.png
Attachments
Leap Year Trapper.livecode.zip
Stack.
(1.11 KiB) Downloaded 125 times

Post Reply