Introduction
If you have a DateTime field in Crystal Reports, you will see Date and Time tab option on the Format Editor when you right-click on the field and select Format Field menu item. From the Date and Time tab, you may select the desired format and select OK.
Custom Date Format
If you do not see the desired format in the available list, you can also use the Customize button that launches Custom Style dialog.
The Custom Style dialog has three tabs - Date and Time, Date, and Time. The Date and Time are used to set the order of date and time using the Order drop-down and the Separator drop-down is used to set the separator.
On the Date tab, you can select the format of the date the way you want. At the bottom, you will see a preview of the date as well.
On-Time tab, you can select the format of time the way you want.
Formatting a Formula for Date
What if you have a formula that has DateTime value and you want to format it? In that case, you do not see the Date and Time tab on the Format Editor.
In this situation, you rely on your own code.
ToText function comes handy to do all formatting. For example, I need a date formatting as follows:
23-Mar-2007
The following formula will do the trick:
ToText({MYFIELDNAME }, "dd-MMM-yyyy")
I can simply move around the formatting to generate other formats. For example, if I need the following format:
Mar 23, 2007
I simply change the formula to the following:
ToText({MYFIELDNAME }, "MMM dd, yyyy")
And if I need format 03/23/07, I change the formula to "MM/dd/yy".