News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

Demo: Perpetual Calendar

Started by balaporte, February 21, 2011, 04:54:23 PM

Previous topic - Next topic

balaporte

Perpetual Calendar

This program takes a date and figures out what day of the week it was on that date. It's probably one of the least exciting pieces of code I've ever written, but I think it's kind of interesting. I used this in a larger program I wrote for work to make my life slightly easier and give me more time for programming some games...

PlayBASIC Code: [Select]
; Calculate the weekday by date

day = Val(CurrentDay$())
month$ = Lower$(Left$(CurrentMonth$(),3))
year = Val(Right$(CurrentYear$(),2))
century = Val(Left$(CurrentYear$(),2))

month$ = "feb"
day = 14
century = 20
year = 11

Select month$
Case "aug"
monthcode = 0
Case "feb", "mar", "nov"
monthcode = 1
Case "jun"
monthcode = 2
Case "sep", "dec"
monthcode = 3
Case "apr", "jun"
monthcode = 4
Case "jan", "oct"
monthcode = 5
Case "may"
monthcode = 6
EndSelect

isleapyearjanfeb = No
If month$ = "jan" Or month$ = "feb"
If Mod(Val(CurrentYear$()),4) = 0
If Mod(Val(CurrentYear$()),100) <> 0
If Mod(Val(CurrentYear$()),400) <> 0
isleapyearjanfeb = Yes
EndIf
EndIf
EndIf
EndIf

weekdaycode = Mod((day + monthcode + year + (year/4) - 2*(Mod(century,4)) - isleapyearjanfeb),7)

select weekdaycode
case 0
weekday$ = "Monday"
case 1
weekday$ = "Tuesday"
case 2
weekday$ = "Wednesday"
case 3
weekday$ = "Thursday"
case 4
weekday$ = "Friday"
case 5
weekday$ = "Saturday"
case 6
weekday$ = "Sunday"
endselect

cls 0

print month$ + "-" + str$(day) + "-" + str$(year) + " is a " + weekday$ + "."

sync

waitkey





balaporte

I just realized this should have been posted to the "Resources" section, so I'll post my other code examples over there from now on...

kevin

#2
 Excellent, that's really handy for personalizing output.

Here's a version that spits out the current date in this format.  Wednesday, 23rd of February


PlayBASIC Code: [Select]
   print GetDate$()
sync
waitkey



Function GetDate$()

day = Val(CurrentDay$())
month$ = Lower$(Left$(CurrentMonth$(),3))
year = Val(Right$(CurrentYear$(),2))
century = Val(Left$(CurrentYear$(),2))

Select month$
Case "aug"
monthcode = 0
Case "feb", "mar", "nov"
monthcode = 1
Case "jun"
monthcode = 2
Case "sep", "dec"
monthcode = 3
Case "apr", "jun"
monthcode = 4
Case "jan", "oct"
monthcode = 5
Case "may"
monthcode = 6
EndSelect

isleapyearjanfeb = No
If month$ = "jan" Or month$ = "feb"
If Mod(Year,4) = 0
If Mod(Year,100) <> 0
If Mod(Year,400) <> 0
isleapyearjanfeb = Yes
EndIf
EndIf
EndIf
EndIf

weekdaycode = Mod((day + monthcode + year + (year/4) - 2*(Mod(century,4)) - isleapyearjanfeb),7)

weekday$ = "0Monday,1Tuesday,2Wednesday,3Thursday,4Friday,5Saturday,6Sunday,"
StartPos=instring(weekday$,str$(weekdaycode))+1
EndPos=instring(weekday$,",",StartPos+1)
WeekDay$=mid$(weekday$,StartPOs,EndPos-StartPos)

Select mod(Day,10)
case 1: TodayDay$="st"
case 2: TodayDay$="nd"
case 3: TodayDay$="rd"
default : TodayDay$="th"
endselect
if Day>9 and Day<20 then TodayDay$="th"
TodayDay$=str$(day)+TodayDay$


ListOFmonths$=",January,February,March,April,May,June,July,August,September,October,November,December"
StartPos=instring(listofmonths$,month$)
EndPos=instring(listofmonths$,",",StartPos+1)

Month$=mid$(ListOFmonths$,StartPOs,EndPos-StartPos)
TodayIs$=Weekday$+", "+TodayDay$+" of "+month$

EndFunction TodayIs$







Related posts

 *  Convert / Get Numeric Date from Date





kevin


Date Parser

   Here's a version that's set up to parse and various date specific tags into a date string. Allowing you greater control over the format of the date. 

   Supported tags


[d] = Numeric Day
[d2] = Numeric Day (padded to 2 digits)
[wds] = Weekday short  (Mon,Tue,Wed,Thur,Fri,Sat,Sun)
[wd] = Weekday (Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday)
[dth] = day suffix st,nd,rd,th

[mth] = Numeric Month
[mth2] = Numeric Month (padded to 2 digits)
[mthname] = First 3 digits of month name
[mthfullname] = Full name of month

[yr] = Numeric Year (4 digit)
[yr2] = Numeric Year (last 2 digits)



balaporte

Nice. I didn't need to go into all that for the simple program I was writing, but this is something I could use in the future.

kevin


It's just variation of the Date$() formatting function that some languages have.  Could build it in, but such things make more sense as a library.