The Daily Newbie - Using the DatePart Function

Submitted on: 1/16/2015 11:54:00 PM
By: Matthew Roberts (from psc cd)  
Level: Beginner
User Rating: By 3 Users
Compatibility: VB 4.0 (16-bit), VB 4.0 (32-bit), VB 5.0, VB 6.0, VB Script, ASP (Active Server Pages) , VBA MS Access
Views: 387
     Explains how to use the DatePart Function in Visual Basic.


Daily Newbie - 05/01/2001

The Daily Newbie

“To Start Things Off Right”

May 8, 2001

Today’s Keyword: DatePart()

Name Derived From: "Part of a Date"

Used for: Getting a part of a date value (i.e. Day, Month, Year, etc.).

VB Help Description: Returns a Variant (Integer) containing the specified part of a given date.

Plain English: Lets you get only one part of a date/time value. For example you can determine what weekday a certain date falls on.

Syntax: Val=DatePart(Part, Date)

Usage:intWeekDay = DatePart("w","01/12/2000")


  • Part - The part of the date you want returned . This can be:
  • s - Seconds
  • n - Minutes
  • h - Hours
  • d - Days
  • y - Day of Year
  • w - Weekday
  • w - Week
  • m - Months
  • q - Quarter
  • yyyy - Year
  • Date - The date that the part is derived from. Example:

    To get the current week within the current year (What week is this for the year? 1-52 )):

    MsgBox DatePart("ww", Date)

    If you have not read the Daily Newbie on how VB stores date format, you may want to review it now by clicking here.

    Today's code snippet returns the Julian date for today.

    Copy & Paste Code:


    MsgBox "Today's Julian Date is: " & DatePart("y",Date) & "/" & DatePart("yyyy",Date)

  • Other 25 submission(s) by this author


    Report Bad Submission
    Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
    This submission should be removed because:

    Your Vote

    What do you think of this article (in the Beginner category)?
    (The article with your highest vote will win this month's coding contest!)
    Excellent  Good  Average  Below Average  Poor (See voting log ...)

    Other User Comments

     There are no comments on this submission.

    Add Your Feedback
    Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)

    To post feedback, first please login.