简体   繁体   中英

Convert string to date in Crystal Reports

I need to convert a string value to a date in a crystal report. As you know Crystal has a handy "in the period of" in the select expert. However I am unable to use it without converting. The string is only four characters (example: 0512) I am having trouble using the date value function since there is no day or a full value for the year. Any help would be appreciated!

Just manipulate the string into the proper format and use the CDate function. It doesn't look like the day number matters to you, so set it to the first of the month.

Assuming that 0512 means May 2012 your code should look something like this:

CDate('01' + '/' + Left({DateObject},2) + '/' + '20' + Right({DateObject},2))

That returns a date of 1/5/12 using your sample value. You can then format it as needed.

cDate(ToText(cDate({?StartDate}),"dd/MM/yyyy")))

I don't think this can be done since Crystal requires the year to be four digits (YYYY). You will always get a "Bad Date Format" error. See this article on the date conversion. You could try and convert it to date on the database end. If your dates are all in the 2000 range, you could get crafty and create a formula that will "build" you a proper date format and then convert it to date.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM