Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Reddit
Contact us
Hide Buttons

VBA-Excel: Date-Time Functions – DatePart()

Description:

The DatePart() func­tion returns the part of given date based on the para­me­ters provided.

Format:

DatePart(interval,date[,firstdayofweek[,firstweekofyear]])

Arguments:
  • inter­val
    • Manda­tory
    • Type: String expression
    • The time inter­val you want as a part of given date

Set­ting Descrip­tion
Yyyy Year
Q Quar­ter
M Month
Y Day of year
D Day
W Week­day
Ww Week
H Hour
N Minute
S Sec­ond

 

  • Date
    • Manda­tory
    • Type: Date
    • Date of which part will be calculated
  • First­day­ofweek
    • Optional
    • Type: Numeric, Text
    • Spec­i­fied the first day of the week, default : Sunday
Con­stant Value Descrip­tion
vbUs­eSys­tem 0 Use the NLS API setting.
vbSun­day 1 Sun­day (default)
vbMon­day 2 Mon­day
vbTues­day 3 Tues­day
vbWednes­day 4 Wednes­day
vbThurs­day 5 Thurs­day
vbFri­day 6 Fri­day
vbSat­ur­day 7 Sat­ur­day
  • First­weeko­fyear
    • Optional
    • Type: Numeric, Text
    • Spec­i­fied the first week of the week.
Con­stant Value Descrip­tion
vbUs­eSys­tem 0 Use the NLS API setting.
vbFirstJan1 1 Start with week in which Jan­u­ary 1 occurs (default).
vbFirst­Four­Days 2 Start with the first week that has at least four days in the new year.
vbFirst­Full­Week 3 Start with first full week of the year.

Exam­ple:

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()

 

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: