Visual FoxPro String to Date Conversion

Nick picture Nick · Aug 9, 2017 · Viewed 7.3k times · Source

how can I convert a string 30-Jul-17 to date format 07/30/17?

Answer

Steve picture Steve · Aug 9, 2017

I don't believe there's a built in way in VFP to parse abbreviated month strings (or even the full month name). If it were me, I'd use a CASE statement for each abbreviated month like so.

lcStringDate = "30-Jul-17"
lcDay = LEFT(lcStringDate, 2)
lcMonth = SUBSTR(lcStringDate, 4, 3)
lcYear = "20"+RIGHT(lcStringDate, 2)

*!* Here you'd need to have code to convert abbreviated
*!* month to a numeric month
DO CASE
    CASE lcMonth = "Jan"
        lcNumericMonth = "1"
    CASE lcMonth = "Feb"
        lcNumericMonth = "2"
    .
    .
    .
ENDCASE

?CTOD(lcNumericMonth+"/"+lcDay+"/"+lcYear)
*!* this would output "07/30/17" if SET CENTURY is OFF
*!* this would output "07/30/2017" if SET CENTURY is ON