Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CTOD Importing XML File

Status
Not open for further replies.

gvanr

IS-IT--Management
Jan 13, 2012
3
US
I'm having a weird issue trying to import a date from an xml file into our software. Some of the dates it reads just fine and puts them into a table and some with the same exact format are coming up blank. The date format is date="2012-01-16" here is the code I am using for the CTOD:
Note that aDate is the field I used to import the XML file.
so aDate = "2012-01-16"
Local cDate, cYear, cMonth, cDay, dDate
cDate = IIF(VARTYPE(aDate) = "C", CHRTRAN(aDate, "-", ""), SPACE(8))
cYear = LEFT(cDate, 4)
cMonth = SUBSTR(cDate, 5, 2)
cDay = RIGHT(cDate, 2)
dDate = CTOD(cMonth + "/" + cDay + "/" + cYear)
So the dDate sometimes displays correctly (01/16/2012) and sometimes displays " / / "
Anyone have this issue or an idea on how to solve it?
Thank you.
 
Well,

in case you get an empty date from CTOD, the original date was insufficient, eg leading zero was missing etc.

If the original string formats are YYYY-MM-DD you don't need to go through separating year, month and day, you can also SET MARK TO "-" and SET DATE TO YMD and then strings like "2012-01-16" are converted via CTOD() directly.

You might also have the case the dates are in YYYY-MM-DD order and the foxpro date is set to YDM or even MDY, so only some dates validate, others not.

Bye, Olaf.
 
Olaf,
Thank you for the suggestion I actually just figured it out.
I had to simply set century on. I'll give your suggestion a try though and see if it works. It's more efficient than my coding. I'm not a programming natural I do more networking but my job wants me to start programming.
 
Hm, set century shouldn't have any influence on ctod(), eg it's not having an effect on the date read in, but just on the display of dates:

[date]
SET MARK TO "-"
SET DATE YMD
SET CENTURY OFF
dDate = CTOD("1999-12-31")
? dDate && displays 99-12-31
SET CENTURY ON
? dDate && displays 1999-12-31
[/code]

A date variable or field always holds a whole date with century, there is no short version of a date, so the unchanged variable dDate is displayeed with and without the century depending on SET CENTURY, but will always contian the century, even if generated from CTOD() while century was set off.

The only other influence SET CENTURY has besides the date display, is how ctod() reads in date strings without a century part, but if century is contained in the source strings the setting has no effect on the conversion to the date type.

Bye, Olaf.
 
Olaf,
Thanks I did that code and it also worked fine.
-GVR
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top