FORMATDATE - add single digit tokens

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
miskox
User
User
Posts: 95
Joined: Sun Aug 27, 2017 7:37 pm
Location: Slovenia

FORMATDATE - add single digit tokens

Post by miskox »

FORMATDATE () has two digit tokens. We in Slovenia should write dates like this:

Code: Select all

day. month. year
There must be a space after the dot and (day and month) numbers should not have a leading zero. Why? Because in a sentence we too write a space after a dot - same rule applies here. Dot is not connected to the letter/number/character on the right.

Code: Select all

2. 8. 2022 (note two spaces)
So maybe

Code: Select all

%d
%m
could be added?

Edit: looks like we are not the only one: https://en.wikipedia.org/wiki/Date_format_by_country

Thanks.
Saso
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: FORMATDATE - add single digit tokens

Post by Little John »

Until that will be built-in into PureBasic, you can use this:

Code: Select all

Procedure.s MyFormatDate (mask$, date.i=-1)
   ; in : mask$: can contain the same tokens as used with PB's FormatDate(),
   ;             plus the following additional ones:
   ;             - %d -->  day   number without leading "0"
   ;             - %m -->  month number without leading "0"
   ;      date : date value in PB's format; -1 for current system date and time
   ; out: mask string with all tokens replaced by the respective date values
   
   If date = -1
      date = Date()
   EndIf
   
   mask$ = FormatDate(mask$, date)
   mask$ = ReplaceString(mask$, "%d", Str(Day(date)))
   mask$ = ReplaceString(mask$, "%m", Str(Month(date)))
   
   ProcedureReturn mask$
EndProcedure


Debug MyFormatDate("%d. %m. %yyyy")
For even more possibilities, see FormatDateEx().
TassyJim
Enthusiast
Enthusiast
Posts: 151
Joined: Sun Jun 16, 2013 6:27 am
Location: Tasmania (Australia)

Re: FORMATDATE - add single digit tokens

Post by TassyJim »

Code: Select all

mydate$ = FormatDate(" %dd.  %mm.  %yyyy",Date())
mydate$ = ReplaceString(mydate$," 0"," ")
Debug mydate$

; all in one line with leading space removed if present
mydate$ = Trim(ReplaceString(FormatDate(" %dd.  %mm.  %yyyy",Date())," 0"," "))
Debug mydate$
miskox
User
User
Posts: 95
Joined: Sun Aug 27, 2017 7:37 pm
Location: Slovenia

Re: FORMATDATE - add single digit tokens

Post by miskox »

Wow! Thanks. That was fast. Of course native solution would be prefered.

Saso
Post Reply