DatePart - Built-in Date & Time Function¶
Purpose¶
DatePart returns the specified part from a date.
Syntax¶
DatePart(str, date)
Arguments¶
| Argument | Required | Description |
|---|---|---|
str |
Yes | Any valid string expression containing the interval |
date |
Yes | Any valid datetime expression of which the part is returned |
Valid interval specifiers for str:
D— dayH— hourN— minuteS— second- 'YYYY' - year
- 'M' - month
- 'Y' - day of year
- 'W' - weekday
Returns¶
Integer
See also¶
- Date
- DateAdd
- DateDiff
- DateValue
- DateSerial
- Day
- Hour
- InAgedToDays
- InLast4WeeksToSunday
- InLast7Days
- InLastFullMonth
- InLastFullWeek
- InLastYearMTD
- InLastYearYTD
- InLeapYear
- Minute
- Month
- MonthName
- Now
- Second
- Time
- TimeSerial
- TimeValue
- WeekAgo
- Weekday
- WeekdayName
- Year
Example¶
// Returns the day number of the order date
Return DatePart("D", {OrderHeader.Order_Date})
Dim iYears
Dim iMonths
Dim iDays
// returns the difference between now and the order date in years, months
// and days. Days can be negative, no correction made on that
Let iYears = DatePart ("YYYY", now()) - DatePart ("YYYY", {OrderHeader.Order_Date})
Let iMonths = DatePart ('M', now()) - DatePart ('M', {OrderHeader.Order_Date})
Let iDays = DatePart ('D', now()) - DatePart ('D', {OrderHeader.Order_Date})
Return ("Difference: " + CStr(iYears) + ' years, ' + CStr(iMonths) + ' months and ' + CStr(iDays) + ' days')
Notes¶
- When the arguments cannot be converted to the specified type, a runtime error is generated.
- The character in
stris not case sensitive.