Search code examples
visual-foxprostring-to-datetime

Visual FoxPro String to Date Conversion


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


Solution

  • 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