Page 1 of 1

ParseDate Suggestion

Posted: Fri Feb 07, 2025 9:05 pm
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

Re: ParseDate Suggestion

Posted: Sat Feb 08, 2025 10:35 pm
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))

Re: ParseDate Suggestion

Posted: Sat Feb 08, 2025 11:42 pm
by AZJIO
You have a special case

Code: Select all

dt$ = "20250207134505"
Debug Left(dt$, 6)
Debug Left(dt$, 4)

Re: ParseDate Suggestion

Posted: Sun Feb 09, 2025 12:29 am
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:

Re: ParseDate Suggestion

Posted: Sun Feb 09, 2025 4:53 pm
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

Pdate ParseDate UpDated

Posted: Fri Jul 04, 2025 2:27 am
by Piero
(see above)