Page 1 of 1 1
Topic Options
#152386 - 2005-11-28 08:08 PM Last day of the month
Stanley Offline
Starting to like KiXtart

Registered: 2004-06-03
Posts: 130
Loc: Upstate NY, USA
Good day all.
I am trying to determine the last day of each month programmatically and this is the best I can come up with.
Thanks to Rob van der Woude for the leap year bit.
I have RTFM and searched here, but no luck.
So I took a stab at it.
Please be gentle.

Code:
 
;Last day of each month
$a = "31","28","31","30","31","30","31","31","30","31","30","31"

; Initialize
$LeapYear = 0
; A leap year is any multiple of 4...
$Test = $Year / 4
$Test = $Test * 4
If $Year = $Test
$LeapYear = 1
EndIf
; ...except If it is a multiple of 100...
$Test = $Year / 100
$Test = $Test * 100
If $Year = $Test
$LeapYear = 0
EndIf
; ...unless it is a multiple of 400!
$Test = $Year / 400
$Test = $Test * 400
If $Year = $Test
$LeapYear = 1
EndIf

;Reset the value in the array
If $LeapYear = 1
$a[1] = 29
EndIf

;Last day of the current month
$ld = $a[@MONTHNO - 1]


_________________________
Taxation WITH representation isn't so hot, either!

Top
#152387 - 2005-11-28 08:26 PM Re: Last day of the month
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
I made a few UDF a while back that would help out with this.

ReturnCTDays(), ReturnCTMonths(), and you will need FlipCTime() and CTime() UDF's

Code:
$days = ReturnCTDays(2004,2)
? ctime($days[ubound($days)])


Top
#152388 - 2005-11-28 10:08 PM Re: Last day of the month
Stanley Offline
Starting to like KiXtart

Registered: 2004-06-03
Posts: 130
Loc: Upstate NY, USA
Thanks Bryce
_________________________
Taxation WITH representation isn't so hot, either!

Top
#152389 - 2005-11-28 10:40 PM Re: Last day of the month
Stanley Offline
Starting to like KiXtart

Registered: 2004-06-03
Posts: 130
Loc: Upstate NY, USA
I golfed down the leapyear bit to this
Code:
 
$Year = @YEAR
;A leap year is any multiple of 4...
If $Year MOD 4 = 0
$LeapYear = 1
EndIf
; ...except If it is a multiple of 100...
If $Year MOD 100 = 0
$LeapYear = 0
EndIf
; ...unless it is a multiple of 400!
If $Year MOD 400 = 0
$LeapYear = 1
EndIf


_________________________
Taxation WITH representation isn't so hot, either!

Top
#152390 - 2005-11-29 12:49 AM Re: Last day of the month
Chris S. Offline
MM club member
*****

Registered: 2002-03-18
Posts: 2368
Loc: Earth
You could look at fnIsDate() for some ideas.
Top
#152391 - 2005-11-29 12:53 AM Re: Last day of the month
Chris S. Offline
MM club member
*****

Registered: 2002-03-18
Posts: 2368
Loc: Earth
Here is another way of doing it...

Code:

$y=@YEAR
$l=(Right($y*25,2)='00') and ((Right($y,2)<>'00') or (Right($y*25,4)='0000'))



Edited by Chris S. (2005-11-29 12:55 AM)

Top
#152392 - 2005-11-29 09:40 AM Re: Last day of the month
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Or even:
Code:
$iLeapYear=Not(@YEAR MOD 400) OR (@YEAR MOD 100) AND Not(@YEAR MOD 4)


Top
#152393 - 2005-11-29 04:15 PM Re: Last day of the month
Stanley Offline
Starting to like KiXtart

Registered: 2004-06-03
Posts: 130
Loc: Upstate NY, USA
The power of the phrase "Golfed it down" amazes me.

But seriously,
Thanks to all for your most excellent suggestions.

I have decided to go with this.

Code:
 
;Create an array of Last day of each month
$mld = "31","28","31","30","31","30","31","31","30","31","30","31"

;Calculate if Leap Year
$LeapYear= Not(@YEAR MOD 400) OR (@YEAR MOD 100) AND Not(@YEAR MOD 4)

;Reset the February value in the array if this is a Leap Year
If $LeapYear = 1
$mld[1] = 29
EndIf

;Get Last day of the current month from $mld array
$ld = $mld[@MONTHNO - 1]


_________________________
Taxation WITH representation isn't so hot, either!

Top
#152394 - 2005-11-30 01:15 AM Re: Last day of the month
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
A lot of KiXtart users play golf as a hobby ;-)
_________________________
There are two types of vessels, submarines and targets.

Top
#152395 - 2005-11-30 10:48 AM Re: Last day of the month
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Quote:

A lot of KiXtart users play golf as a hobby ;-)




...and some of us can never let it go :
Code:
;Create an array of Last day of each month
$mld = "31","28","31","30","31","30","31","31","30","31","30","31"

; Determine number of days in February
$mld[1]=28+(Not(@YEAR MOD 400) OR (@YEAR MOD 100) AND Not(@YEAR MOD 4))

;Get Last day of the current month from $mld array
$ld = $mld[@MONTHNO-1]


Top
#152396 - 2005-11-30 11:00 AM Re: Last day of the month
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
And one even plays Lonkenized Golf. Not meant for the weak, only for the top korg-ers.

Just yanking your chain Jooel
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#152397 - 2005-11-30 05:35 PM Re: Last day of the month
Stanley Offline
Starting to like KiXtart

Registered: 2004-06-03
Posts: 130
Loc: Upstate NY, USA
Thank you Richard.
And thanks to all.

Stanley

If the automobile had followed the same development cycle as the computer, a Rolls-Royce would today cost $100, get a million miles per gallon, and explode once a year, killing everyone inside.
_________________________
Taxation WITH representation isn't so hot, either!

Top
#152398 - 2005-12-01 11:01 PM Re: Last day of the month
kholm Offline
Korg Regular
*****

Registered: 2000-06-19
Posts: 714
Loc: Randers, Denmark
Mini Golf ??

Slightly modified version of Richard's code

Code:
;Create an array of Last day of each month with buieldin leapyar check
$mld = 31,28+(Not(@Year Mod 400) Or (@Year Mod 100) And Not(@Year Mod 4)),31,30,31,30,31,31,30,31,30,31

;Get Last day of the current month from $mld array
$ld = $mld[@MonthNo-1]


I got curious:
Can you build an array of integer values for last day of month, with encapsulated code for february?

At least with KiX 4.51 you can! (previous code works)

-Erik

Top
#152399 - 2005-12-02 10:39 AM Re: Last day of the month
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Hah! That's delightful.

Funny how obvious these things are once someone else has done the work

Top
#152400 - 2005-12-02 03:35 PM Re: Last day of the month
Chris S. Offline
MM club member
*****

Registered: 2002-03-18
Posts: 2368
Loc: Earth
I can't resist...

Code:

;Create an array of Last day of each month with builtin leapyar check
$o=0,3-(Not(@Year Mod 400) Or (@Year Mod 100) And Not(@Year Mod 4)),0,1,0,1,0,0,1,0,1,0

;Get Last day of the current month from $o array
$d=31-$o[@MonthNo-1]


Top
Page 1 of 1 1


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 525 anonymous users online.
Newest Members
batdk82, StuTheCoder, M_Moore, BeeEm, min_seow
17885 Registered Users

Generated in 0.166 seconds in which 0.112 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org