|
Be the first user to complete this post
|
Add to List |
VBA-Excel: Date-Time Functions – DatePart()
Description:
The DatePart() function returns the part of given date based on the parameters provided.
Format:
DatePart(interval,date[,firstdayofweek[,firstweekofyear]])
Arguments:
- interval
- Mandatory
- Type: String expression
- The time interval you want as a part of given date
| Setting | Description |
| Yyyy | Year |
| Q | Quarter |
| M | Month |
| Y | Day of year |
| D | Day |
| W | Weekday |
| Ww | Week |
| H | Hour |
| N | Minute |
| S | Second |
- Date
- Mandatory
- Type: Date
- Date of which part will be calculated
- Firstdayofweek
- Optional
- Type: Numeric, Text
- Specified the first day of the week, default : Sunday
| Constant | Value | Description |
| vbUseSystem | 0 | Use the NLS API setting. |
| vbSunday | 1 | Sunday (default) |
| vbMonday | 2 | Monday |
| vbTuesday | 3 | Tuesday |
| vbWednesday | 4 | Wednesday |
| vbThursday | 5 | Thursday |
| vbFriday | 6 | Friday |
| vbSaturday | 7 | Saturday |
- Firstweekofyear
- Optional
- Type: Numeric, Text
- Specified the first week of the week.
| Constant | Value | Description |
| vbUseSystem | 0 | Use the NLS API setting. |
| vbFirstJan1 | 1 | Start with week in which January 1 occurs (default). |
| vbFirstFourDays | 2 | Start with the first week that has at least four days in the new year. |
| vbFirstFullWeek | 3 | Start with first full week of the year. |
Example:
Function FnDatePart()
Dim strDate
strDate = Now
strDayPart = "Days Part of current date time is " & DatePart("d", strDate)
strMonthPart = "Months Part of current date time is " & DatePart("m", strDate)
strYearPart = "Years Part of current date time is " & DatePart("yyyy", strDate)
strQuaterPart = "Quaters Part of current date time is " & DatePart("q", strDate)
strHoursPart = "Hours Part of current date time is " & DatePart("h", strDate)
strMintuesPart = "Minutes Part of current date time is " & DatePart("n", strDate)
strSecondsPart = "Seconds Part of current date time is " & DatePart("s", strDate)
MsgBox strDayPart & vbCrLf & strMonthPart & vbCrLf & strYearPart & vbCrLf & strQuaterPart & vbCrLf & strHoursPart & vbCrLf & strMintuesPart & vbCrLf & strSecondsPart
End Function

Also Read:
- VBA-Excel: Date-Time Functions – DateSerial()
- VBA Excel – Refer to Multiple Ranges : Union Method
- VBA-Excel: Select and Activate Cells - Activate
- VBA Excel - Cells, Ranges and Offset : Range
- VBA-Excel: Date-Time Functions - Timer()