Be the first user to complete this post

  • 0
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
    • Manda­tory
    • Type: String expression
    • The time interval you want as a part of given date
SettingDescription
YyyyYear
QQuarter
MMonth
YDay of year
DDay
WWeekday
WwWeek
HHour
NMinute
SSecond
  • 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
ConstantValueDescription
vbUseSystem0Use the NLS API setting.
vbSunday1Sunday (default)
vbMonday2Monday
vbTuesday3Tuesday
vbWednesday4Wednesday
vbThursday5Thursday
vbFriday6Friday
vbSaturday7Saturday
  • Firstweekofyear
    • Optional
    • Type: Numeric, Text
    • Specified the first week of the week.
ConstantValueDescription
vbUseSystem0Use the NLS API setting.
vbFirstJan11Start with week in which January 1 occurs (default).
vbFirstFourDays2Start with the first week that has at least four days in the new year.
vbFirstFullWeek3Start 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
DatePart()
DatePart()



Also Read:

  1. VBA-Excel: String Functions – Mid()
  2. VBA-Excel: Date-Time Functions – Date(), Now() and Time()
  3. VBA-Excel: Date-Time Functions – DateValue()
  4. VBA-Excel: String Functions – Replace()
  5. VBA Excel - Cells, Ranges and Offset : Range