#176655 - 2007-05-31 11:12 PM
Re: show or print the days of any month in any year
[Re: Adolfo]
|
Glenn Barnas
KiX Supporter
   
Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
|
Download the TimeConvert UDF and try this:
$NumDays = SubStr(TimeConvert(TimeConvert(first_day_of_next_month) - 86400), 8, 2)
Such, if I want to know how many days were in Feb this year, I'd run
$NumDays = SubStr(TimeConvert(TimeConvert(2007/3/1) - 86400), 8, 2)
This obtains the cTime value for midnight of March first, subtracts 86400 (one day in seconds), and converts back to YYYY/MM/DD HH:MM:SS format. The SubStr pulls out the day value, which will always be an accurate last day of the month, which, of course, is the number of days in the month.
I used to use an array for most months, and calculate Feb, but this method is so easy I don't bother anymore.
Glenn
PS - cTime is a date/time value represented as the number of seconds that elapsed from "the epoch" - a specific point in time. The UDF uses 1/1/1970 by default, but can be changed to any date in history. Makes it very easy to calculate the time between dates. The UDF takes some handy arguments, like "now" and "today" to simplify timing events.
_________________________
Actually I am a Rocket Scientist!
|
|
Top
|
|
|
|
#176725 - 2007-06-04 03:31 PM
Re: Calculate the first day of any month and year and shows a Calendar
[Re: Glenn Barnas]
|
Adolfo
Fresh Scripter
   
Registered: 2007-01-25
Posts: 49
Loc: Cali, CO
|
Thanks Glenn
;FUNCTION nCalculateFirstDayOfMonth()
;
;ACTION Calculate the first day of any month and year and shows a Calendar
;
;PARAMETERS Set these variables
; $nMyMonth=6
; $nMyYear=2007
; and
; $nGetFirstDayOfMonth=nCalculateFirstDayOfMonth($nMyMonth,$nMyYear)
;
;RETURNS 0=Sunday
; 1=Monday
; .
; 6=Saturday
Break ON
$nMyMonth=6
$nMyYear=2007
$nGetFirstDayOfMonth=nCalculateFirstDayOfMonth($nMyMonth,$nMyYear)
nDisplayCalendar($nGetFirstDayOfMonth,$nMyMonth,$nMyYear)
Function nCalculateFirstDayOfMonth($nMonth, $nYear)
Dim $nJan, $nFeb, $nMar, $nApr, $nMay, $nJun, $nJul, $nAug, $nSep, $nOct, $nNov, $nDec, $nCountDays
;Days of every month
$nJan=31
if $nYear mod 4 = 0
$nFeb=29
else
$nFeb=28
endif
$nMar=31
$nApr=30
$nMay=31
$nJun=30
$nJul=31
$nAug=31
$nSep=30
$nOct=31
$nNov=30
$nDec=31
;Calculate Number of Days
$nCountDays= ((($nYear-1)/4)*366)+((($nYear-1)-($nYear-1)/4)*365)
SELECT
CASE $nMonth=1
$nCountDays=$nCountDays+1
CASE $nMonth=2
$nCountDays=$nCountDays+$nJan+1
CASE $nMonth=3
$nCountDays=$nCountDays+$nJan+$nFeb+1
CASE $nMonth=4
$nCountDays=$nCountDays+$nJan+$nFeb+$nMar+1
CASE $nMonth=5
$nCountDays=$nCountDays+$nJan+$nFeb+$nMar+$nApr+1
CASE $nMonth=6
$nCountDays=$nCountDays+$nJan+$nFeb+$nMar+$nApr+$nMay+1
CASE $nMonth=7
$nCountDays=$nCountDays+$nJan+$nFeb+$nMar+$nApr+$nMay+$nJun+1
CASE $nMonth=8
$nCountDays=$nCountDays+$nJan+$nFeb+$nMar+$nApr+$nMay+$nJun+$nJul+1
CASE $nMonth=9
$nCountDays=$nCountDays+$nJan+$nFeb+$nMar+$nApr+$nMay+$nJun+$nJul+$nAug+1
CASE $nMonth=10
$nCountDays=$nCountDays+$nJan+$nFeb+$nMar+$nApr+$nMay+$nJun+$nJul+$nAug+$nSep+1
CASE $nMonth=11
$nCountDays=$nCountDays+$nJan+$nFeb+$nMar+$nApr+$nMay+$nJun+$nJul+$nAug+$nSep+$nOct+1
CASE $nMonth=12
$nCountDays=$nCountDays+$nJan+$nFeb+$nMar+$nApr+$nMay+$nJun+$nJul+$nAug+$nSep+$nOct+$nNov+1
CASE 1
?"Invalid Month"
EXIT
ENDSELECT
;DETERMINE DAY OF THE WEEK
SELECT
CASE $nCountDays mod 7 = 0
$nCalculateFirstDayOfMonth=7
CASE $nCountDays mod 7 = 1
$nCalculateFirstDayOfMonth=1
CASE $nCountDays mod 7 = 2
$nCalculateFirstDayOfMonth=2
CASE $nCountDays mod 7 = 3
$nCalculateFirstDayOfMonth=3
CASE $nCountDays mod 7 = 4
$nCalculateFirstDayOfMonth=4
CASE $nCountDays mod 7 = 5
$nCalculateFirstDayOfMonth=5
CASE $nCountDays mod 7 = 6
$nCalculateFirstDayOfMonth=6
ENDSELECT
ENDFUNCTION
Function nDisplayCalendar($nFirstDayOfMonth,$nCalendarMonth,$nCalendarYear)
dim $nNoOfDaysofMonth, $iRow, $iColomn
$iRow=6
CLS
SELECT
CASE $nCalendarMonth=1
$nNoOfDaysOfMonth=31
at($iRow-4,4) "January of "$nCalendarYear
CASE $nCalendarMonth=2 and $nCalendarYear mod 4 = 0
$nNoOfDaysOfMonth=29
at($iRow-4,4) "February of "$nCalendarYear
CASE $nCalendarMonth=2 and $nCalendarYear mod 4 <> 0
$nNoOfDaysOfMonth=28
at($iRow-4,4) "February of "$nCalendarYear
CASE $nCalendarMonth=3
$nNoOfDaysOfMonth=31
at($iRow-4,4) "March of "$nCalendarYear
CASE $nCalendarMonth=4
$nNoOfDaysOfMonth=30
at($iRow-4,4) "April of "$nCalendarYear
CASE $nCalendarMonth=5
$nNoOfDaysOfMonth=31
at($iRow-4,4) "May of "$nCalendarYear
CASE $nCalendarMonth=6
$nNoOfDaysOfMonth=30
at($iRow-4,4) "June of "$nCalendarYear
CASE $nCalendarMonth=7
$nNoOfDaysOfMonth=31
at($iRow-4,4) "July of "$nCalendarYear
CASE $nCalendarMonth=8
$nNoOfDaysOfMonth=31
at($iRow-4,4) "August of "$nCalendarYear
CASE $nCalendarMonth=9
$nNoOfDaysOfMonth=30
at($iRow-4,4) "September of "$nCalendarYear
CASE $nCalendarMonth=10
$nNoOfDaysOfMonth=31
at($iRow-4,4) "October of "$nCalendarYear
CASE $nCalendarMonth=11
$nNoOfDaysOfMonth=30
at($iRow-4,4) "November of "$nCalendarYear
CASE $nCalendarMonth=12
$nNoOfDaysOfMonth=31
at($iRow-4,4) "December of "$nCalendarYear
ENDSELECT
$iColumn=$nFirstDayOfMonth
at($iRow-1,4) COLOR w+/b "S M T W T F S " COLOR w+/n
for $i=1 to $nNoOfDaysOfMonth
at($iRow,$iColumn*4) $i
if $iColumn mod 7 = 0
$iRow=$iRow+1
$iColumn=0
endif
$iColumn=$iColumn+1
next
EndFunction
gets $
Edited by Adolfo (2007-06-04 05:29 PM)
|
|
Top
|
|
|
|
#176776 - 2007-06-05 07:22 PM
Re: Calculate the first day of any month and year and shows a Calendar
[Re: Adolfo]
|
Glenn Barnas
KiX Supporter
   
Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
|
Adolfo,
I found by testing that your code does not work for dates prior to 1901. It's also pretty complex code, in my opinion.
This code works for any month from October 1752 and beyond. (Prior to 10/1752, leap years were not observed, and 12 days were actually removed from September in 1752 to adjust the calendar.) The code could be adjusted to compensate for this, but that was more than I wanted to do - how often would we need a calendar prior to 1752 anyway? 
; DisplayCalMonth(date)
; Glenn Barnas
; Displays a calendar month, highlighting a specific day
; Accepts a single date argument in the format YYYY/MM/DD
;
Function DisplayCalMonth($_MyDate)
Dim $_MName ; Array of month names
Dim $_Offset, $_LOff ; Left offset count, string of spaces
Dim $_aMyDate ; Array of date values
Dim $_DCurr, $_DFirst, $_DLast ; Defined day of month, first weekday, last day of month
Dim $_Header ; header string - Month YYYY
Dim $_Tag, $_Day ; Loop counter, Day index
; Month name array
$_MName = 'January','February','March','April','May','June','July','August','September','October','November','December'
; Left offset is 3 spaces, up to 40 are permitted
$_Offset = 12
$_LOff = Left(' ', $_Offset)
; Split the supplied date value into an array
$_aMyDate = Split($_MyDate, '/')
$_dCurr = $_aMyDate[2] ; save the defined day of the month for later reference
$_aMyDate[2] = '1' ; Change supplied date to reference the first day of the month
; Determine first weekday (0=Sunday)
; Use TimeConvert to return cTime, divide by seconds/day to determine days, offset by 4 and use mod for a 0-6 DOW value
$_DFirst = ((TimeConvert(Join($_aMyDate, '/') + ' 00:00:00', 0) / 86400)) Mod 7
; Get number of days in the current month - change to first of next month and subtract 1 day
; Then get the Day portion. Increment the year if we're in December!
If Val($_aMyDate[1]) = 12
$_aMyDate[0] = Val($_aMyDate[0]) + 1
$_aMyDate[1] = 1
Else
$_aMyDate[1] = Val($_aMyDate[1]) + 1
EndIf
; timeConvert date to cTime, subtract 1 day in seconds, and convert back to YYYY/MM/DD format, trim time
$_DLast = Split(Split(TimeConvert(TimeConvert(Join($_aMyDate, '/') + ' 00:00:00', 0) - 86400, 0), '/')[2], ' ')[0]
; Create the header - format is "Month Year" plus an appropriate left offset
$_Header = $_MName[Val($_aMyDate[1]) - 2] + ' ' + $_aMyDate[0]
$_Header = Left(' ', $_Offset + ((28 - Len($_Header)) / 2)) + $_Header
; Display the calendar for the defined month, highlighting the specific day
$_Header ?
$_LOff COLOR w+/b ' S M T W T F S ' COLOR w+/n
$_Tag = 0 ; loop control
$_Day = 1 ; day value
While $_Tag >= 0
If $_Tag <= $_DFirst ; has first day occurred?
' ' ; if not, print blank entry
Else
If $_Day = $_DCurr COLOR y+/r EndIf ; highlight current day
Right(' ' + CStr($_Day), 3) ' ' ; otherwise print formatted day entry
$_Day = $_Day + 1 ; and increment day count
COLOR w+/n ; restore color
EndIf
If $_Tag Mod 7 = 0 @CRLF $_LOff EndIf ; next line, then print offset
$_Tag = $_Tag + 1
If $_Day > $_DLast $_Tag = -1 EndIf ; close the loop when we display the last day
Loop
? ?
EndFunction
I'm not particularly fond of UDFs that perform any kind of output, but this is kind of an exception - I wrote this as a UDF so multiple calendars can be easily tested/displayed.
DisplayCalMonth('1900/1/1') DisplayCalMonth('1864/4/14') DisplayCalMonth('2007/6/5')
Glenn
_________________________
Actually I am a Rocket Scientist!
|
|
Top
|
|
|
|
#176939 - 2007-06-13 06:23 AM
Re: Calculate the first day of any month and year and shows a Calendar
[Re: Glenn Barnas]
|
Bryce
KiX Supporter
   
Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
|
I made a UDF for just this a while ago,
ReturnCTDays()
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 1376 anonymous users online.
|
|
|