VBA - DatePart Function

A Function, which returns the specific part of the given date.
DatePart(interval,date[,firstdayofweek[,firstweekofyear]]) 

Interval − A required parameter. It can take the following values.

d - day of the year.
m - month of the year
y - year of the year
yyyy - year
w - weekday
ww - week
q - quarter
h - hour
n - minute
s - second


Interval − A required parameter. It can take the following values.
d - day of the year.
d - day of the year.
m - month of the year
m - month of the year
y - year of the year
y - year of the year
yyyy - year
yyyy - year
w - weekday
w - weekday
ww - week
ww - week
q - quarter
q - quarter
h - hour
h - hour
n - minute
n - minute
s - second
s - second
Date1 − A required parameter.
Date1 − A required parameter.
Firstdayofweek − An optional parameter. Specifies the first day of the week. It can take the following values.

0 = vbUseSystemDayOfWeek - Use National Language Support (NLS) API setting
1 = vbSunday - Sunday 
2 = vbMonday - Monday
3 = vbTuesday - Tuesday
4 = vbWednesday - Wednesday
5 = vbThursday - Thursday
6 = vbFriday - Friday
7 = vbSaturday - Saturday


Firstdayofweek − An optional parameter. Specifies the first day of the week. It can take the following values.
0 = vbUseSystemDayOfWeek - Use National Language Support (NLS) API setting
0 = vbUseSystemDayOfWeek - Use National Language Support (NLS) API setting
1 = vbSunday - Sunday 
1 = vbSunday - Sunday 
2 = vbMonday - Monday
2 = vbMonday - Monday
3 = vbTuesday - Tuesday
3 = vbTuesday - Tuesday
4 = vbWednesday - Wednesday
4 = vbWednesday - Wednesday
5 = vbThursday - Thursday
5 = vbThursday - Thursday
6 = vbFriday - Friday
6 = vbFriday - Friday
7 = vbSaturday - Saturday
7 = vbSaturday - Saturday
Firstdayofyear − An optional parameter. Specifies the first day of the year. It can take the following values.

0 = vbUseSystem - Use National Language Support (NLS) API setting
1 = vbFirstJan1 - Start with the week in which January 1 occurs (default)
2 = vbFirstFourDays - Start with the week that has at least four days in the new year
3 = vbFirstFullWeek - Start with the first full week of the new year


Firstdayofyear − An optional parameter. Specifies the first day of the year. It can take the following values.
0 = vbUseSystem - Use National Language Support (NLS) API setting
0 = vbUseSystem - Use National Language Support (NLS) API setting
1 = vbFirstJan1 - Start with the week in which January 1 occurs (default)
1 = vbFirstJan1 - Start with the week in which January 1 occurs (default)
2 = vbFirstFourDays - Start with the week that has at least four days in the new year
2 = vbFirstFourDays - Start with the week that has at least four days in the new year
3 = vbFirstFullWeek - Start with the first full week of the new year
3 = vbFirstFullWeek - Start with the first full week of the new year
Add a button and add the following function.
Private Sub Constant_demo_Click()
   Dim Quarter as Variant
   Dim DayOfYear as Variant
   Dim WeekOfYear as Variant
  
   Date1 = "2013-01-15"
   Quarter    = DatePart("q", Date1)
   
   msgbox("Line 1 : " & Quarter)
   DayOfYear  = DatePart("y", Date1)
   
   msgbox("Line 2 : " & DayOfYear)
   WeekOfYear = DatePart("ww", Date1)
   
   msgbox("Line 3 : " & WeekOfYear)
   msgbox("Line 4 : " & DatePart("m",Date1))  
End Sub
When you execute the above function, it produces the following output.
Line 1 : 1
Line 2 : 15
Line 3 : 3
Line 4 : 1 


    101 Lectures 
     6 hours 

 Pavan Lalwani

    41 Lectures 
     3 hours 

 Arnold Higuit

    80 Lectures 
     5.5 hours 

 Prashant Panchal

    25 Lectures 
     2 hours 

 Prashant Panchal

    26 Lectures 
     2 hours 

 Arnold Higuit

    92 Lectures 
     10.5 hours 

 Vijay Kumar Parvatha Reddy
 Print
 Add Notes
 Bookmark this page