Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Making CTOD() work in Europe
Message
From
16/11/2018 15:38:55
 
 
To
16/11/2018 13:54:50
Dragan Nedeljkovich (Online)
Now officially retired
Zrenjanin, Serbia
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
01663465
Message ID:
01663479
Views:
44
>>>>Hi,
>>>>
>>>>As I go through my program, I see many places where I convert a string to date, using CTOD(), hard-coded with MM/DD/YYYY. But if the date is (for example), British ("DD/MM/YYYY") it won't work.
>>>>
>>>>How do you suggest I change all places that use CTOD() from hard-coded use of American date system to another?
>>>
>>>You don't, the ctod() respects the set date format.
>>>Unless you have date constants.
>>
>>I know that ctod() respects the set date format; but I didn't :). For example, I could have in my program:
>>
>>This.r_nFirstDay = DOW(CTOD(STR(This.r_nMonth,2,0)+"/01/"+STR(This.r_nYear,4,0)))
>>
>>The above will not work since when I have SET DATE to BRITISH, the string should be Date Month Year.
>
>You should have used dow(date(This.r_nYear, This.r_nMonth, 1)). Much simpler.

Before we were using VFP, the DATE() function didn't take parameters to allow you to construct date values. I wrote the following "brute force" procedure to construct date values:
PARAMETERS nDy,nMn,nYr, dRet
PRIVATE dChk
dChk = CTOD("01/02/03")        && use dummy date to determine proper ordering
DO CASE
    CASE MONTH(dChk)=1
        IF DAY(dChk)=2
            dRet = CTOD(ALLTRIM(STR(nMn,2))+"/"+ALLTRIM(STR(nDy,2))+"/"+ALLTRIM(STR(nYr,4)))
        ELSE
            dRet = CTOD(ALLTRIM(STR(nMn,2))+"/"+ALLTRIM(STR(nYr,4))+"/"+ALLTRIM(STR(nDy,2)))
        ENDIF
    CASE MONTH(dChk)=2
        IF DAY(dChk)=1
            dRet = CTOD(ALLTRIM(STR(nDy,2))+"/"+ALLTRIM(STR(nMn,2))+"/"+ALLTRIM(STR(nYr,4)))
        ELSE
            dRet = CTOD(ALLTRIM(STR(nYr,4))+"/"+ALLTRIM(STR(nMn,2))+"/"+ALLTRIM(STR(nDy,2)))
        ENDIF
    OTHERWISE
        IF DAY(dChk)=1
            dRet = CTOD(ALLTRIM(STR(nDy,2))+"/"+ALLTRIM(STR(nYr,4))+"/"+ALLTRIM(STR(nMn,2)))
        ELSE
            dRet = CTOD(ALLTRIM(STR(nYr,4))+"/"+ALLTRIM(STR(nDy,2))+"/"+ALLTRIM(STR(nMn,2)))
        ENDIF
ENDCASE
You might wonder why I didn't simply change the date format to a known value then construct the date value. I had my reasons -- at the time we were using QuickSilver for development, and we were using ON TIMER events -- so I had to be *very careful* about altering any global settings.

One thing I do recall that became a potential "gotcha" is that before the "strict" format (where you used the caret followed by year, month then day) was defined for using curly-brace date constants -- the interpretation was dependent at the time you compiled your code. If the program you were working on allowed the user to select the date format, and you were testing within the IDE, the date setting may not always be in a predictable state (unless you ALWAYS made sure to execute statements to reset stuff like date format explicitly before compiling). My co-workers got hit by that one a few times.
Previous
Reply
Map
View

Click here to load this message in the networking platform