ParseDate Suggestion

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
swhite
Enthusiast
Enthusiast
Posts: 785
Joined: Thu May 21, 2009 6:56 pm

ParseDate Suggestion

Post by swhite »

Hi

When I use FormatDate("%yyyy%mm",Date()) I get "202502" if the date was actually Feb 7, 2025. So it would be nice if the ParseDate worked similarly. ParseDate("%yyyy%mm%dd","20250207134505") should return a value equivalent to Feb 7, 2025 at midnight instead it returns -1. Similarly
ParseDate("%yyyy%mm","20250207134505") would return Feb 1 2025 and ParseDate("%yyyy","20250207134505") would return Jan 1 2025.

Simon
Simon White
dCipher Computing
User avatar
Piero
Addict
Addict
Posts: 863
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: ParseDate Suggestion

Post by Piero »

Code: Select all

macro pdate(a,b)
   ParseDate(a,Left(b,Len(a)-CountString(a,"%")))
EndMacro

s.s="20250207134505"
Debug ParseDate("%yyyy%mm%dd",s)
Debug pdate("%yyyy%mm%dd",s)
Debug FormatDate("%yy/%mm/%dd", pdate("%yyyy%mm%dd",s))
Debug FormatDate("%yy/%mm/%dd", pdate("%yyyy%mm"   ,s))
AZJIO
Addict
Addict
Posts: 2141
Joined: Sun May 14, 2017 1:48 am

Re: ParseDate Suggestion

Post by AZJIO »

You have a special case

Code: Select all

dt$ = "20250207134505"
Debug Left(dt$, 6)
Debug Left(dt$, 4)
User avatar
Piero
Addict
Addict
Posts: 863
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: ParseDate Suggestion

Post by Piero »

AZJIO wrote: Sat Feb 08, 2025 11:42 pmYou have a special case
What do you mean?

Code: Select all

macro pdate(a,b)
   ParseDate(a,Left(b,Len(a)-CountString(a,"%")))
EndMacro

s.s="2025---02 07 13 45 05"
Debug ParseDate("%yyyy---%mm",s) ; -1
Debug FormatDate("%yy/%mm/%dd",pdate("%yyyy---%mm",s)) ; 25/02/01
Edit: oh, I think I understood; you meant that a simple Left() would be the fastest solution for his specific example… but, dear AZJIO, this is a feature request/wish, like "GET A MAC!" :wink: :mrgreen:
User avatar
Piero
Addict
Addict
Posts: 863
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: ParseDate Suggestion

Post by Piero »

Just for (speed and) fun; it's raining here :cry:

Code: Select all

Procedure.q Pdate(a.s,b.s)
   Protected c=Len(a)-CountString(a,"%"),lb=Len(b) ; <— protected stuff
   If lb>c
      ProcedureReturn ParseDate(a,Left(b,c))
   ElseIf lb<c
      Protected i,j,k,l ; <— additional protected stuff
      j=Len(a)
      for i=1 to j
         if Mid(a,i,1)="%"
            k+1
         Else
            l+1
            If l=lb
               Break
            EndIf
         EndIf
      Next
      ProcedureReturn ParseDate(Left(a,lb+k),b)
   EndIf
   ProcedureReturn ParseDate(a,b)
EndProcedure

s.s="20250207134505"; longer
Debug ParseDate("%yyyy%mm%dd",s) ; error -1
Debug Pdate("%yyyy%mm%dd",s)
Debug FormatDate("%yy/%mm/%dd", Pdate("%yyyy%mm%dd",s))
Debug ""
s="10251122" ; shorter
Debug ParseDate("%dd%yy%mm%hh%ii",s) ; error -1
Debug Pdate("%dd%yy%mm%hh%ii",s)
Debug FormatDate("%yy/%mm/%dd", Pdate("%dd%yy%mm%hh%ii",s))
Last edit: fixed/improved "shorter case" and protected vars declaration
Last edited by Piero on Tue Jul 08, 2025 4:49 pm, edited 1 time in total.
User avatar
Piero
Addict
Addict
Posts: 863
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Pdate ParseDate UpDated

Post by Piero »

(see above)
Post Reply