The DatePart function in VBA returns a part (day, month, week, etc.) for the specified date and/or time.

Syntax

Parameter

Interval (required) – This parameter represents a string specifying the interval to be used.

It can take the following values:

  • “d” – Day of month (1-31)
  • “h” – Hours
  • “n” – Minutes
  • “m” – Months
  • “q” – Quarters
  • “s” – Seconds
  • “ww” – Weeks
  • “yyyy” – Years

Date1 (required) – This parameter represents a date value, returning a part of it.

FirstDayOfWeek (optional) – This parameter represents the weekday that should be used as the first day of the week.

It can take the following values:

  • vbUseSystemDayOfWeek – The first day of the week is as specified in your system settings. The default value is set to vbSunday.
  • vbSunday – Sunday
  • vbMonday – Monday
  • vbTuesday – Tuesday    
  • vbWednesday – Wednesday
  • vbThursday – Thursday
  • vbFriday – Friday
  • vbSaturday – Saturday

FirstWeekOfYear (optional) – This parameter represents the week that should be used as the first week of the year. The default value is set to vbFirstJan1.

It can take the following values:

  • vbSystem – It signifies the first week of the year is as specified in your system settings
  • vbFirstJan1 – It signifies the week in which Jan 1st occurs.
  • vbFirstFourDays – It signifies the first week that contains at least four days in the new year.
  • vbFirstFourDays – It signifies the first full week in the new year

Return

Example 1

Output

1
12
2020
VBA DatePart Function

Example 2

Output

21
5
40

Example 3

Output

338
4
50
4
VBA DatePart Function

Pin It on Pinterest

Share This