Date processing in VB

Posted on 2004-10-27
Last Modified: 2010-04-17
Given a date in mm/dd/yyyy format. how can i find the no of days on that month.  is there any built in functions available in VB...??
Question by:bushairkp
    LVL 10

    Expert Comment

    Hi bushairkp,
    If it is a date type, then the difference in days between that date and a month hence will give you the answer:
    Days = datediff("d", <givendate>, dateadd("m",1, <givendate>))

    Regards .. Alan
    LVL 10

    Expert Comment

    If it's actually a date type, the format doesn't matter
    .. Alan

    Author Comment

    i am telling you ...i want to find the no of days of a month.
    i mean ..suppose our function name is xxxxx
    if i passed a value to (01/01/2002) then it should return 31 , and if i passed a value 02/15/2002(feb) then it should return 28. if the value is 02/15/2004 (feb-leap year) it should return 29.....

    do you know any functions intented for that ...??
    LVL 10

    Accepted Solution

    The above statement will do that.
    Given 01/01/2002 as <givendate>:
    dim Givendate as date, NextMonth as date
    Givendate = cDate("01/01/2002")
    NextMonth = dateadd("m",1, Givendate) will give a date that will format as either 01/02/2002 or 02/01/2002 (depending on US or GB locale)
    datediff("d",Givendate,NextMonth) (GB) will give the number of days between 01/01/2002 and 02/01/2002. i.e. the number of days in January 2002.

    .. Alan
    LVL 4

    Expert Comment

    Here's an alternative (if you're looking for other options.)  The following code uses the DateSerial function:

        Format$(Day(DateSerial(Year(givenDate), month(givenDate) + 1, 0)))

    (It assumes that givenDate is a Date type.  Use cDate(dateString) as specified by ADSaunders if you need to do a String to Date conversion.)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Suggested Solutions

    Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
    Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now