Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

9.10 DatePart()

DatePart( )

The DatePart() function returns a number representing the part of the date that you specify using the interval parameter. These intervals are listed in Table 9-11. Pass the interval as the first parameter and the date as the second parameter.

‘Get the current quarter
Formula = DatePart("q", CurrentDate) ‘Returns a number 1 - 4

Use interval “w” to display the weekday and it returns a number from 1 to 7. By default, Sunday is represented by a 1. The optional third parameter designates which day of the week is considered the first day of the week. If you passed this parameter crMonday, then Sunday is represented by a 7.

Use “ww” to display the week and it returns a number from 1 to 53. By default, the first week is the week that has January 1st in it. Use the optional fourth parameter to designate a different way of determining the first week of the year. There are two other methods to do this. The first is method specifies that the first week is the one with at least four days in it. The second method specifies the first week as the first one to have seven full days in it. Table 9-12 lists the different constants that are used to specify the first week of the year parameter.

What happens if you specify the first week to be the first one with seven full days, and you pass it a date of 2/1/2002 that only has five days in the week? Does DatePart() return a 0? No, it returns 53 to let you know that the date falls before the first official week of the year.

Since the third and fourth parameters are both optional, if you want to specify the fourth parameter, then you are also required to specify the third parameter (the first day of the week). Although by default this is crSunday, you must still pass it to the function in order to be able to use the fourth parameter. In this circumstance the third parameter is ignored and the DatePart() function always assumes Sunday to be the first day of the week.

Table 9-12. First Week of the Year Constants

Constant Description
crFirstJan1 The week that has January 1st.
crFirstFourDays The first week that has at least four days in it.
crFirstFullWeek The first week that has seven days in it.