We help IT Professionals succeed at work.

Change date to MMDDYYY

phantomcom asked
I need to take a date (my control source name is eventbook.bdate) and convert the date to simply a number. In other words, eventbook.dbate might be "10/02/2011".  I need to convert it simply to 10022011.  After doing that i need to combine it with a name (eventbook.cust).

What i am trying to accomplish is printing a report to a PDF file but i want the file name to be the customer's name + the date. For instance Joe Smoe10022011.pdf.  I was trying to accomplish this with the code below, but i can't seem to get the date to change simply to a number without a mismatch error.

set procedure to Print2PDF.prg additive
loPDF = createobject("Print2PDF")
contractemaildate = Month(eventbook.bdate) + DAY(eventbook.bdate) + YEAR(eventbook.dbate)
custcontract = TRIM(Eventbook.cust) + contractemaildate
IF isnull(loPDF)
            =messagebox("Could not setup PDF class!",48,"Error")
            return .f.
      WITH loPDF
            .cINIFile             = "c:\Event Administrator\Print2PDF.ini"
            .cOutputFile       = "c:\Event Administrator\emailcontracts\" + custcontract + ".pdf"
            .cReport            = "c:\eveadmin\djpro\contract.frx"
            &&.cExtraRptClauses =
            llResult             = .Main()

      IF!llResult or loPDF.lError
            =messagebox("This error ocurred creating the PDF:"+chr(13)+;
            alltrim(loPDF.cError),48,"Error Message")

I figure this is something easy that I just can't figure out.
Watch Question

Founder, Software Engineer, Data Scientist
I would suggest name + DTOS(eventbook.bdate) which gives better sort order YYYYMMDD.

If you wish to have it as requested then this:

contractemaildate = STRTRAN(STR(Month(eventbook.bdate),2) + STR(DAY(eventbook.bdate),2) + STR(YEAR(eventbook.dbate),2),' ','0')
I've found efficient date to string conversion formula based on CHRTRAN. I don't know the source but the idea is very clever:

? CHRTRAN('56781234','12345678',DTOS(DATE()))  && MMDDYYYY
? CHRTRAN('56/78/1234','12345678',DTOS(DATE()))  && MM/DD/YYYY
? CHRTRAN('56/78/34','12345678',DTOS(DATE()))  && MM/DD/YY

? CHRTRAN('56/78','12345678',DTOS(DATE()))  && MM/DD

Adapted to your problem:

contractemaildate = CHRTRAN('56781234', '12345678', DTOS(eventbook.bdate))


Although it might be the long way around, i was able to get custcontract loaded correctly with the following code:

contractemaildatea = Month(eventbook.bdate)
contractemaildateb = DAY(eventbook.bdate)
contractemaildatec = YEAR(eventbook.bdate)
custcontract = ALLTRIM(Trim(Eventbook.cust)+LTRIM(STR(contractemaildatea))+LTRIM(STR(contractemaildateb))+LTRIM(STR(contractemaildatec)))

However, i am now having a problem at the line:

I am getting an operator/operand type mismatch error.


Sorry, forgot to put in the new problem line:

.cOutputFile       = "c:\Event Administrator\emailcontracts\"+ custcontract +".pdf"
CaptainCyrilFounder, Software Engineer, Data Scientist

This is a more elegant way:

custcontract =TRANSFORM(MONTH(eventbook.bdate),'@L 99') + TRANSFORM(DAY(eventbook.bdate),'@L 99') + TRANSFORM(YEAR(eventbook.bdate),'@L 9999')

The error "operator/operand type mismatch error. " is caused by the fact you are attempting to mix text and numbers in one expression which is not allowed without appropriate conversions in FoxPro.

But the custcontract varaible seems to be correct text... Do you have any table open? If the table has custcontract column of the type different from text then it must fail.

Try to use the explicit memory variable prefix:

.cOutputFile       = "c:\Event Administrator\emailcontracts\"+ m.custcontract +".pdf"

If it will does not help then you have to place SET STEP ON into your code and investigate what's hapenning.
CaptainCyrilFounder, Software Engineer, Data Scientist

pcelba, this is not the first time you post a CHRTRAN solution and all of them are very intelligent solutions :-)
Unfortunately, I am not as good to invent such solutions myself... Somebody is always faster. :-)

CHRTRAN can be used to format even datetime values in various ways. I like such solutions.


The mystery is partially solved. On the first time through, the code works fine and the pdf is created as needed. However, if i run the code a second time, then the operator/operand type mismatch error comes up. If i completely exit out of VFP and start again, the code works again correctly the 1st time. After that the error message.

Am i correct in thinking that something (a variable) in the actual print2pdf function is not releasing and when i run it a second time it runs into this variable?


Whats odd is that i use print2pdf in another part of my program where i create employee schedules and then email them to the email address on file for each employee. That works over and over again. It does not generate the mismatch error.  I haven't changed the original coding of the print2pdf in any way.
This could mean the bug is really in print2pdf.prg.

In such case you have to compile it with debug info and trace the code until the error occurs.

SET STEP ON  is your only friend.


After further testing the problem doesn't seem to be with print2pdf. It is a problem with this line of code:
 .cReport = "c:\eveadmin\djpro\contract.frx"

It doesn't like that report. If i just use a blank test report, i do not get the mismatch.  Just baffled a bit what the report has in it that is causing the error.   The report works just fine when called in a different part of my program. Any ideas?
CaptainCyrilFounder, Software Engineer, Data Scientist

If could be the that variables are getting changed when doing to printout. Make sure they are LOCAL variables and not PUBLIC or PRIVATE.

Try printing the type of variables and values before you assign the file name or before the error.

? TYPE('variable'), variable && where variable is what you are testing
The line you are marking as incorrect does not contain any bug...  BUT this assignment can invoke some processing inside the print2pdf object e.g. _Assign method call.

Does the print2pdf contain some Assign methods?

print2pdf can also process the report file and if some expression "does not fit" to print2pdf's framework then the error may appear. I am almost sure the bug is in print2pdf.


Is there anything else (free) that you might suggest instead of using print2pdf ?  I would not be opposed to having the code send the report to a pdf printer,have the code assign the file name, have the user hit save, and then have the code save the file name in the necessary folder.  So it wouldn't have to be entirely automated, as long as the file name and saved location are automatic.

Let me know if you have any suggestions