how can I convert a string 30-Jul-17
to date format 07/30/17
?
Asked
Active
Viewed 9,379 times
2
3 Answers
2
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

Steve
- 506
- 6
- 16
-
1Thanks for the idea. I was hoping for an easier way, but this will work. – Nick Aug 09 '17 at 15:54
1
To slightly extend on what Steve provided, you could make it as a function and call repeatedly...
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

DRapp
- 47,638
- 12
- 72
- 142
0
Try this:
"a" is your date string
DATE(2000+VAL(SUBSTR(a,8,2)),int((AT(SUBSTR(UPPER(a),4,3),"JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC")-1)/3)+1,VAL(SUBSTR(a,1,2)))

FredK
- 1