Advanced Date Formatting

Share your advanced PureBasic knowledge/code with the community.
Mohawk70
Enthusiast
Enthusiast
Posts: 404
Joined: Thu May 11, 2006 1:04 am
Location: Florida, USA

Advanced Date Formatting

Post by Mohawk70 »

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
[/size]


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
[/size]
Last edited by Mohawk70 on Sun Feb 06, 2011 10:19 pm, edited 1 time in total.
HP Z800 Workstation
CPU : Dual Xeon 5690 3.46GHz
RAM : 96GB RAM ( 8GB x 12 )
PSU : 1100W
GPU : NVIDIA RTX 3050 8GB
STORAGE : 9TB
(4) 2TB Seagate IronWolf Pro HDD
(1) 1TB Samsung 870 EVO SSD
User avatar
Vera
Addict
Addict
Posts: 858
Joined: Tue Aug 11, 2009 1:56 pm
Location: Essen (Germany)

Re: Advanced Date Formatting

Post by Vera »

Hello Mohawk70,

thank you for sharing :)

It's a nice collection of formated date outputs and to me a clear study to learn from.

cheers ~ Vera

btw: there's a small mischief in your pasted code, as for a hard break of line 41(/42 )

Code: Select all

Macro LongMonth(theMonth)
  StringField("January February March April May June July August September October November
December",theMonth," ")
EndMacro
[/size]
C64
Enthusiast
Enthusiast
Posts: 151
Joined: Sat Dec 18, 2010 4:40 am

Re: Advanced Date Formatting

Post by C64 »

Code: Select all

Macro Weekday(theDate)
  DayOfWeek(theDate) + 1
EndMacro
This is totally wrong. DayOfWeek() returns 0-6, and you're adding 1 to it? Right now it's Friday here, but this macro says it's Saturday. And when it's Saturday (6), it's going to return 7, which has no day associated with it.
User avatar
Vera
Addict
Addict
Posts: 858
Joined: Tue Aug 11, 2009 1:56 pm
Location: Essen (Germany)

Re: Advanced Date Formatting

Post by Vera »

Hi C64,

well, you're right as for the DayOfWeek() command itself. But in this case +1 is needed because StringField() starts by 1 (not 0).

I only stumbled across this the other day, when building a formated date output myself.

Besides - when testing the lib it returns the correct weekdays for me - doesn't it for you ?

greetings ~ Vera
C64
Enthusiast
Enthusiast
Posts: 151
Joined: Sat Dec 18, 2010 4:40 am

Re: Advanced Date Formatting

Post by C64 »

Yes, you are correct. My bad.
Mohawk70
Enthusiast
Enthusiast
Posts: 404
Joined: Thu May 11, 2006 1:04 am
Location: Florida, USA

Re: Advanced Date Formatting

Post by Mohawk70 »

Vera wrote:Hello Mohawk70,

thank you for sharing :)

It's a nice collection of formated date outputs and to me a clear study to learn from.

cheers ~ Vera

btw: there's a small mischief in your pasted code, as for a hard break of line 41(/42 )

Code: Select all

Macro LongMonth(theMonth)
  StringField("January February March April May June July August September October November
December",theMonth," ")
EndMacro
[/size]
fixed !
HP Z800 Workstation
CPU : Dual Xeon 5690 3.46GHz
RAM : 96GB RAM ( 8GB x 12 )
PSU : 1100W
GPU : NVIDIA RTX 3050 8GB
STORAGE : 9TB
(4) 2TB Seagate IronWolf Pro HDD
(1) 1TB Samsung 870 EVO SSD
Post Reply