Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

6.10 Extracting Date Parts

DatePart(interval, date, firstdayofweek, firstweekofyear)

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

//Get the current quarter
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 argument designates which day of the week is considered the first day of the week. It effectively shifts the numeric representation so that the day you passed is treated as the first day. If you passed this argument crTuesday, then Tuesday is represented by a 1 and Sunday becomes 6.

Use “ww” to display which week of the year a particular date falls in. 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 argument to designate a different way of determining the first week of the year. There are two other methods to do this. The first 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 6-14 lists the different constants that are used to specify the first week of the year argument.

Table 6-14. 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.

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 arguments are both optional, if you want to specify the fourth argument, then you are also required to specify the third argument (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 argument. In this circumstance, the third argument is ignored and the DatePart() function always assumes Sunday to be the first day of the week.

MonthName(date, firstdayofweek)

WeekDayName(weekday, abbreviate, firstdayofweek)

WeekDay(date, firstdayofweek)

Just like the DatePart() function, these functions are given a date value and they return part of the date. The difference is that these functions are more specialized than the DatePart() function.

The MonthName() function is passed a number representing the month and it returns the name of the month fully spelled out. There is an optional second argument that lets you specify whether it should be abbreviated to three letters or not. Pass True to the second argument to get the abbreviated name. By default, this is False and it returns the full name.

The WeekDayName() function is passed a number representing the day of the week and it returns the name of the day fully spelled out. Just like MonthName(), you can pass True to the optional second argument to get the 3 letter abbreviation.

The WeekDay() function is passed a date and it returns a number.

Both the WeekDayName() and WeekDay() functions use a number to represent the day of the week. By default, this number is a 1 for Sunday and a 7 for Saturday. As discussed for the DatePart() function, you can shift this number by specifying a different first day of the week. If you passed crMonday to the function, then Sunday is represented by a 7. You pass this as the third argument for the WeekDayName() function and as the second argument for the WeekDay() function.

//Demonstrate using the first day of the week argument
WeekDayName(2, True, crMonday); //Returns “Tue” for Tuesday
WeekDay(#1/6/2002#, crMonday); //Returns 7 b/c it is a Sunday