One of the most important and widely used functions that we encounter while working with **VBA **is the **DatePart** function of **VBA**. Today in this article, Iâ€™ll show you how you can use the **DatePart** function of **VBA** with proper examples and illustrations.

Itâ€™ll return **20**, the day of the date **â€ś20/11/2019â€ť**.

**Table of Contents**hide

**Download Practice Workbook**

Download this practice book to exercise the task while you are reading this article.

**Introduction to the DatePart Function of VBA in Excel**

**â§**** Function Objective:**

The **DatePart** function returns a specific portion of a given date stated by a specific interval. There are a total of **10** intervals available for use within the **DatePart** function.

Each interval returns a distinct portion of the date.

**â§**** Syntax:**

**â§**** Arguments:**

Argument | Required / Optional | Explanation |
---|---|---|

Interval | Required | Denotes the specific portion of the date thatâ€™ll be returned. |

Date | Required | The date from which a specific part will be returned. |

FirstDayofWeek | Optional | Specifies the first day of the week. Optional. The default is Sunday. |

FirstWeekofYear | Optional | Specifies the first week of the year. Default is the week that contains January 01. |

There are a total of **10** intervals available for use within the **DatePart** function.

Interval | Return Value |
---|---|

d | Day of the Date |

m | Month of the Date |

yyyy | Year of the Date |

q | Quarter of the Date |

y | Day of the Year of the Date |

w | Weekday of the Date |

ww | Week Number of the Year of the Date |

h | Hour of the Time |

n | Minute of the Time |

s | Second of the Time |

And a total of **8** constant values can be used as the **FirstDayofWeek**.

FirstDayofWeek | Explanation |
---|---|

vbUseSystem | Uses the NLS API setting to dictate the first day of the week. |

vbSunday | Sets Sunday as the first day of the week. |

vbMonday | Â Sets Monday as the first day of the week. |

vbTuesday | Â Sets Tuesday as the first day of the week. |

vbWednesday | Â Sets Wednesday as the first day of the week. |

vbThursday | Â Sets Thursday as the first day of the week. |

vbFriday | Â Sets Friday as the first day of the week. |

vbSaturday | Â Sets Saturday as the first day of the week. |

Also, a total of **4** constant values can be used as the **FirstWeekofYear**.

FirstWeekofYear | Explanation |
---|---|

vbUseSystem | Uses the NLS API setting to dictate the first week of the year. |

vbFirstJan1 | Sets the week with January 1 as the first week of the year. |

vbFirstFourDays | Sets the week that holds the first four days of the year as the first week. |

vbFirstFullWeek | Sets the first full week of the year as the first week. |

**â§**** Return Value:**

Returns the part of the given date specified by the given interval.

**7 Examples to Use the DatePart Function of VBA in Excel**

Letâ€™s see the **Datepart function** in detail with a few examples.

**1. Extract the Day from a Given Date Using the DatePart Function of VBA**

First of all, letâ€™s return the day from a given date using the **DatePart** function. You have to use the interval **â€śdâ€ť** for this purpose.

The line of code to extract the day will be:

`Day_Number = DatePart("d", "20/11/2019")`

Run this code and itâ€™ll return the day of the date **20/11/2019**. Itâ€™s **20**.

**Read More:** **How to Get the Current Date in VBA**

**2. Find out the Month of a Given Date Using the DatePart Function of VBA**

Now weâ€™ll return the month from a given date using the **DatePart** function. You have to use the interval **â€śmâ€ť** for this purpose.

The line of code will be:

`Month_Number = DatePart("m", "20/11/2019")`

Run this code and itâ€™ll return the month of the date **20/11/2019**. Itâ€™s **11**.

**Read More: How to Use EoMonth in Excel VBA**

**3. Separate the Year from a Given Date Using the DatePart Function of VBA**

Next, weâ€™ll return the year from a given date using the **DatePart** function. You have to use the interval **â€śyyyyâ€ť** for this purpose.

The line of code will be:

`Year_Number = DatePart("yyyy", "20/11/2019")`

Run this code and itâ€™ll return the year of the date **20/11/2019**. Itâ€™s **2019.**

**Read More: How to Use Year Function in Excel VBA**

**4. Find out the Quarter of a Given Date Using the DatePart Function of VBA**

Now weâ€™ll return the quarter from a given date using the **DatePart** function. You have to use the interval **â€śqâ€ť** for this purpose.

The line of code will be:

`Quarter_Number = DatePart("q", "20/11/2019")`

Run this code and itâ€™ll return the quarter of the date **20/11/2019**. Itâ€™s **4.**

**Read More: How to Get the Day of Week Using VBA**

**Similar Readings**

**Use VBA DateSerial Function in Excel (5 Easy Applications)****How to Use Excel VBA MONTH Function (7 Suitable Examples)****Use VBA DateValue Function in Excel (6 Examples)****How to Use the VBA DateAdd Function in Excel****Now and Format Functions in Excel VBA (4 Examples)**

**5. Find out the Day of Year of a Given Date Using the DatePart Function of VBA**

Now weâ€™ll perform a new thing. Weâ€™ll return the day number of the year of a given date. We have to use the interval **â€śyâ€ť** for this purpose.

The line of code will be:

`Day_of_the_Year = DatePart("y", "20/11/2019")`

Run this code and itâ€™ll return the day of the year of the date **20/11/2019**. Itâ€™s **324.**

**Read More: How to Sort Dates in Excel by Year**

**6. Extract out the Weekday of a Given Date Using the DatePart Function of VBA**

We can also extract out the weekday of a given date using the **DatePart** function of **VBA**.

But to accomplish this, you have to specify the first day of the week. Itâ€™s optional. Default is **Sunday**.

Letâ€™s find out the weekday of the date **20/11/2019** considering **Saturday** as the first day. The interval will be **â€śwâ€ť** and the first day of the week will be** vbSaturday**.

The line of code will be:

`Weekday_Number = DatePart("w", "20/11/2019",vbSaturday)`

Run this code and itâ€™ll return the weekday of the date **20/11/2019** considering **Saturday** as the first day of the week.

Itâ€™s **5**. That means it was **Wednesday**.

**7. Extract out the Number of Week of a Given Date Using the DatePart Function of VBA**

Finally, weâ€™ll find out the number of the week of a given date using the **DatePart** function. You have to use the interval **â€śwwâ€ť** for this purpose.

The number of weeks is counted considering the week with J**anuary 1** as the first week by default. If you want to change it, you have to set the argument **FirstweekofYear** according to **table 4**Â of this article.

Letâ€™s find out the week number of the date 20/11/2019 considering the week with January 1 as the first week (default). Then the line of code will be:

`Week_Number = DatePart("ww", "20/11/2019")`

Run this code and itâ€™ll return the week number of the date **20/11/2019** considering the week with **January 1** as the first week.

Itâ€™s **47**.

**Read More: Excel VBA to Find Week Number (6 Quick Examples)**

**Summary**

In short, the** DatePart** function takes a **date** and returns a specific portion of the date. It can be the day, month, year, day of the year, weekday, week number, etc. It can also return the hour, minute, and second if there is time attached with the date.

**Conclusion**

Therefore, using these methods you can use the **DatePart** function of **VBA** to return a particular portion of any date according to your need. Do you have any questions? Feel free to ask us.