VFP Date Format

Hi all. I have a date in 09/18/2012 format that I need to change to 2012-09-08.
I prefer not to change my SET DATE in order to do this.

I've tried DTOS, which gives the correct format, but is missing the - separators.
Who is Participating?
Olaf DoschkeSoftware DeveloperCommented:
Nice, Pavel.

You should perhaps explain the idea a bit. It's essential you use different digits here, not all 9, because each digit represents a unique position in the first parameter. Putting it in the second parameter of CHRTRAN() you say it should be replaced, and it's replaced with the corresponding position of the third parameter, the string you want to format.

That's a nice idea. You could also choose eg
? CHRTRAN("abcd-ef-gh", "abcdefgh", DTOS(DATE()))

Of course if you want to add text to the format, like in the third sample, the replacement chars need to be something different than letters. But there are very many chars and it's your choice.

Bye, Olaf.

PS: While CHRTRAN is very elegant and TRANSFORM is meant for string formatting, still the simple solution of jharkins is not lesser. If you understand it easier, it's for you, because the performance of a single transformation doesn't count in comparison of you coming back later to the code needing to maintain it, change it, and therefore understand it.
ldDate = date(2012,09,18)
?TRANSFORM(DTOS( ldDate ),"@R 9999/99/99")
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Olaf DoschkeSoftware DeveloperCommented:
peter almost had it, just replace the mask:
ldDate = date(2012,09,08)
?TRANSFORM(DTOS( ldDate ),"@R 9999-99-99")

With TRANSFORM and @R you can do most formatting as you like. and inject chars into a string, as long as those chars don't have a specific format meaning, like 9 has.

You can also add hyphens to a EAN barcode that way for example:

lcBarcode = "123456789012"

X in this case stands for any char from a string, not only digits. So you can also ad spaces to split a lengthy hex string into hex bytes:

lchexdigits = "ab34f2b7c26e"

Bye, Olaf.
DTOS() is even better in conjunction with CHRTRAN() because it allows various formatting patterns without SET DATE and SET CENTURY change:

? CHRTRAN("1234-56-78", "12345678", DTOS(DATE()))
? CHRTRAN("56/78/34", "12345678", DTOS(DATE()))
? CHRTRAN("Year: 1234, Month: 56", "12345678", DTOS(DATE()))
formadmirerAuthor Commented:
Thanks all.

I wound up going with
ldDate = CHRTRAN("abcd-ef-gh", "abcdefgh", DTOS(ldDate))
because I like the fact that it allows for other formatting options, plus, like Olaf said, it's easy for me to look at and know what is going on. When I come back 6 mos from now I'll still understand what's happening.

I also really like
?TRANSFORM(DTOS( ldMyDate ),"@R 9999-99-99")  because it's clean and concise. The problem is I don't understand '@R' and what it does, so I opted not to use it.

BTW, I wound up using the TRANSFORM barcode example to format my serial number, so thanks for that as well!
R is formatting code.  to use it in the transform you put the @ in front of it.  But format codes are most powerful when used in form text boxes.  Allows the display and entry of formatted text but you save it only saves the data.

ie Canadian sin numbers are 999-999-999 and using an input string of 999-999-99 and format code R it saves as 999999999 but the user sees 999-999-999  


As for your problem, I like the pcelba's solution the best too.
Olaf DoschkeSoftware DeveloperCommented:
Yes, "@R ..." makes transform work as a textbox does with format="R" and Inputmask ="..."


You set cFormatcodes to a combination of the explained single format codes and their meaning, in most cases @R is a good choice for the beginning of cFormatcodes.

Another single format code often helpful is for hex conversion:
? Transform(255,"@0")
? 0xFF

There are many useful formatcodes to combine, but yes, it's not very transparent. If you knew fox dos with @...GET PICTURE clause you'd know how to make use of Transform...


Bye, Olaf.
Just to inform you the CHRTRAN() is not my original idea but I don't remember where I've seen it the first time... I just have to agree it's perfect.
Olaf DoschkeSoftware DeveloperCommented:
The CHRTRAN() it's not so easy to understand, if you're used to format masks using 9999, which is quite common also in different languages, but then it's easier to lookup what CHRTRAN() does than reading the TRANSFORM() reference to find what @R means.

So maybe it's not only elegant but even easier to grasp. What's also nice is, how easy you can reorder the input, as your second example shows. The only hard to remember thing is, for a normal use of CHRTRAN your main input is the first parameter, not the third. You can get confused by that usage.

In regard to date string formatting I still prefer format codes like Y,M and D, but that's just covering data parts and is not as universal.

Bye, Olaf.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.