代码之家  ›  专栏  ›  技术社区  ›  Nick

Visual FoxPro字符串到日期的转换

  •  2
  • Nick  · 技术社区  · 7 年前

    30-Jul-17 截止日期格式 07/30/17 ?

    3 回复  |  直到 7 年前
        1
  •  2
  •   Steve    7 年前

    我认为VFP中没有内置的方法来解析缩写的月份字符串(甚至是完整的月份名称)。如果是我的话,我会用一个案例陈述来描述每个这样的月份。

    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
    
        2
  •  1
  •   DRapp    7 年前

        lcDate = "30-Jul-17"
        ? DMYToDate( lcDate )
    
        lcDate = "15-August-17"
        ? DMYToDate( lcDate )
    
        lcDate = "29-Feb-17"    && No such feb 29, 2017
        ? DMYToDate( lcDate )
    
        lcDate = "32-Mar-17"    && no month 32 days
        ? DMYToDate( lcDate )
    
    
    FUNCTION DMYToDate
    LPARAMETERS lcTryDate
        local lnDay, lnYear, lcMonth, tmpDate, ldNewDate
        lnDay = INT( VAL( LEFT( lcTryDate, 2 )))
        lnYear = 2000 + INT(VAL(RIGHT( lcTryDate, 2 )))
        lcMonth = SUBSTR( lcTryDate, 4, 3 )
    
        */ Cycle through each month with arbitrary start date...
        tmpDate = DATE(2000,1,1)
        DO WHILE YEAR( tmpDate ) < 2001
            IF ATC( lcMonth, CMONTH( tmpDate ) ) = 1
                EXIT
            ENDIF 
            tmpDate = GOMONTH( tmpDate, 1 )
        ENDDO 
        IF YEAR( tmpDate ) > 2000
            */ No such month found, return empty date.
            RETURN CTOD( ""  )
        ENDIF 
    
        TRY
            ldNewDate = DATE( lnYear, MONTH( tmpDate ), lnDay )
        CATCH 
            ldNewDate = CTOD( "" )
        ENDTRY 
    
        RETURN ldNewDate
    ENDFUNC
    
        3
  •  0
  •   FredK    7 年前

    试试这个:

    “a”是您的日期字符串

    DATE(2000+VAL(SUBSTR(a,8,2)),int((AT(SUBSTR(UPPER(a),4,3),"JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC")-1)/3)+1,VAL(SUBSTR(a,1,2)))