Advanced Date Formatting
Posted: Fri Feb 04, 2011 5:06 am
Code: Select all
; File : Advanced_Date_Formatting.pbi
; Description : Advanced Date Formatting
; Author : David Tupponce
; Date : Thu 03-Feb-2011
; Forum Handle : Mohawk70
; Copyright : Copyright © 2011 David C. Tupponce
;
; License
;
; Permission is hereby granted, free of charge, to any person obtaining a copy
; of this software and associated documentation files (the "Software"), to deal
; in the Software without restriction, including without limitation the rights
; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
; copies of the Software, and to permit persons to whom the Software is furnished
; to do so, subject to the following conditions:
;
; The above copyright notice and this permission notice shall be included in
; all copies or substantial portions of the Software.
;
; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
; THE SOFTWARE.
;
EnableDebugger
Global sOut.s
Macro Weekday(theDate)
DayOfWeek(theDate) + 1
EndMacro
Macro ShortMonth(theMonth)
StringField("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec",theMonth," ")
EndMacro
Macro LongMonth(theMonth)
StringField("January February March April May June July August September October November December",theMonth," ")
EndMacro
Macro ShortDay(theDay)
StringField("Sun Mon Tue Wed Thu Fri Sat",theDay," ")
EndMacro
Macro LongDay(theDay)
StringField("Sunday Monday Tuesday Wednesday Thursday Friday Saturday",theDay," ")
EndMacro
Macro FormattedDate(param1,param2,param3)
PeekS(@sOut,GetFormattedDate(param1,param2,param3),#PB_Ascii)
EndMacro
Procedure.i GetFormattedDate(theDate.i,mode.i,seperater.i)
sOut.s = ""
Protected sep.s = StringField("/ . - ",seperater.i," ")
Select mode.i
Case 1
sOut.s = FormatDate("%mm"+sep+"%dd"+sep+"%yy",theDate)
Case 2
sOut.s = FormatDate(ShortDay(Weekday(theDate))+" %mm"+sep+"%dd"+sep+"%yy",theDate)
Case 3
sOut.s = FormatDate(LongDay(Weekday(theDate))+" %mm"+sep+"%dd"+sep+"%yy",theDate)
Case 4
sOut.s = FormatDate("%dd"+sep+ShortMonth(Month(theDate))+sep+"%yy",theDate)
Case 5
sOut.s = FormatDate(ShortDay(Weekday(theDate))+" %dd"+sep+ShortMonth(Month(theDate))+sep+"%yy",theDate)
Case 6
sOut.s = FormatDate(LongDay(Weekday(theDate))+" %dd"+sep+ShortMonth(Month(theDate))+sep+"%yy",theDate)
Case 7
sOut.s = FormatDate("%dd"+sep+LongMonth(Month(theDate))+sep+"%yy",theDate)
Case 8
sOut.s = FormatDate(ShortDay(Weekday(theDate))+" %dd"+sep+LongMonth(Month(theDate))+sep+"%yy",theDate)
Case 9
sOut.s = FormatDate(LongDay(Weekday(theDate))+" %dd"+sep+LongMonth(Month(theDate))+sep+"%yy",theDate)
Case 10
sOut.s = FormatDate("%mm"+sep+"%dd"+sep+"%yyyy",theDate)
Case 11
sOut.s = FormatDate(ShortDay(Weekday(theDate))+" %mm"+sep+"%dd"+sep+"%yyyy",theDate)
Case 12
sOut.s = FormatDate(LongDay(Weekday(theDate))+" %mm"+sep+"%dd"+sep+"%yyyy",theDate)
Case 13
sOut.s = FormatDate("%dd"+sep+ShortMonth(Month(theDate))+sep+"%yyyy",theDate)
Case 14
sOut.s = FormatDate(ShortDay(Weekday(theDate))+" %dd"+sep+ShortMonth(Month(theDate))+sep+"%yyyy",theDate)
Case 15
sOut.s = FormatDate(LongDay(Weekday(theDate))+" %dd"+sep+ShortMonth(Month(theDate))+sep+"%yyyy",theDate)
Case 16
sOut.s = FormatDate("%dd"+sep+LongMonth(Month(theDate))+sep+"%yyyy",theDate)
Case 17
sOut.s = FormatDate(ShortDay(Weekday(theDate))+" %dd"+sep+LongMonth(Month(theDate))+sep+"%yyyy",theDate)
Case 18
sOut.s = FormatDate(LongDay(Weekday(theDate))+" %dd"+sep+LongMonth(Month(theDate))+sep+"%yyyy",theDate)
Case 19
sOut.s = FormatDate(ShortMonth(Month(theDate))+" "+"%dd,%yyyy",theDate)
Case 20
sOut.s = FormatDate(ShortDay(Weekday(theDate))+" "+ShortMonth(Month(theDate))+" %dd,%yyyy",theDate)
Case 21
sOut.s = FormatDate(LongDay(Weekday(theDate))+" "+ShortMonth(Month(theDate))+" %dd,%yyyy",theDate)
Case 22
sOut.s = FormatDate(LongMonth(Month(theDate))+" "+"%dd,%yyyy",theDate)
Case 23
sOut.s = FormatDate(ShortDay(Weekday(theDate))+" "+LongMonth(Month(theDate))+" %dd,%yyyy",theDate)
Case 24
sOut.s = FormatDate(LongDay(Weekday(theDate))+" "+LongMonth(Month(theDate))+" %dd,%yyyy",theDate)
EndSelect
ProcedureReturn Len(sOut.s)
EndProcedure
Code: Select all
;Test Code (Advanced_Date_Formatting.pb)
XIncludeFile "Advanced_Date_Formatting.pbi"
today.i = Date()
For i = 1 To 24
Debug RSet(Str(i),2,"0") + " : " + FormattedDate(today,i,3)
If i % 3 = 0; for better readability for this example only
Debug " "
EndIf
Next