It’s relatively easy to get dates formatted in the .NET world, and the same is equally true when it comes to presenting them in C1 Reports. Most of us are familiar with the standard short date (01/05/2012) or long date (01 May 2012) formats but achieving the correctly formatted ordinal used in formal correspondence (1st May 2012) is a little more difficult. There is no inbuilt ordinal function in .NET and there isn’t one in VBScript either. Given however that there are at most only 31 days to play with (the majority of which will make use of the same ordinal) it shouldn’t be too difficult to achieve this. Thinking about ordinals in English we would need the following variants;
- 1, 21 and 31 will require ‘st’
- 2 and 22 will require ‘nd’
- 3 and 23 will require ‘rd’
- Everything else requires ‘th’
VBScript already has a number of inbuilt functions that can be used with dates, one of which is Day() that will retrieve the day part of any given date, so on the face of it a simple Select Case statement or function using that ought to do the trick. Unfortunately though whilst VBScript does supports both Select Case statements and functions, C1 Reports’ implementation of it doesn’t. However all is not lost, and although this is a little long winded it does produce the desired result. In the above code the database field containing the date on which we want an ordinal to be used is ‘CertificateDate’. The code begins by using the inbuilt Day() function on that field to determine the actual day of the month which is then used to calculate which ordinal to use. Field3 is a calculated field that has been added to the report that will be used to show our formatted date. This block of code has been added to the ‘On Format’ event of the section of the report in which we want the date to appear. It will work equally well in the ‘On Print’ event as well. Finally the WeekDayName() function has been used as well so that we end up with the first day of May written in the report like this; Tuesday, 1st May 2012. There are just a couple of things to note about this. The astute amongst you will have realized that this has been formatted for European date formatting. Altering the month and day is simple enough to do. Month() and WeekDayName() will respect local settings on your computer, but the ordinals illustrated are those that would be used in English. If you your first language is not English and you feel happy to contribute your knowledge to other C1 users then please feel free to add your language specific ordinals as a comment to this blog. This does not produce the ordinal in superscript, which would be the absolutely correct way to present it. It is possible to achieve that in a C1Report, but it involves a bit of work and would increase the rendering time of the document. There is just one last point to bear in mind. If applied to a very large report there would be a significant hit in the time taken for it to render. Realistically you would probably not want to implement this for all reports (typical examples where it might be applicable would be in the production of very formal documents (certificates or letters of intent (you haven’t paid so we’re going to sue you!)) and those would probably be parametrized and so by definition would have a relatively small number to render.