dcsimg
Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Language: VB5,VB6,VBS
Expertise: Intermediate
Aug 7, 2000

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


An enhanced VB DatePart function

Here is an enhanced version of VB DatePart function: the first parameter is now an enumerated type, so easier to use. DatePartEx is 4-5 times faster than DatePart and supports also two new intervals: MonthName and WeekdayName.

Enum DateTimePart
    dtYear
    dtQuarter
    dtMonth
    dtMonthName
    dtShortMonthName
    dtDay
    dtDayOfTheYear
    dtWeekday
    dtWeekdayName
    dtShortWeekdayName
    dtWeekOfTheYear
    dtHour
    dtMinute
    dtSecond
End Enum

' Enhanced VB DatePart function
Function DatePartEx(ByVal Interval As DateTimePart, newDate As Date, _
    Optional FirstDayOfWeek As VbDayOfWeek = vbSunday, Optional FirstWeekOfYear _
    As VbFirstWeekOfYear = vbFirstJan1) As Variant
    ' Select the right and faster date/time function
    Select Case Interval
        Case dtYear
            DatePartEx = Year(newDate)
        Case dtQuarter
            DatePartEx = DatePart("q", newDate, FirstDayOfWeek, FirstWeekOfYear)
        Case dtMonth
            DatePartEx = Month(newDate)
        Case dtMonthName
            DatePartEx = MonthName(Month(newDate), False)
        Case dtShortMonthName
            DatePartEx = MonthName(Month(newDate), True)
        Case dtDay
            DatePartEx = Day(newDate)
        Case dtDayOfTheYear
            DatePartEx = DatePart("y", newDate, FirstDayOfWeek, FirstWeekOfYear)
        Case dtWeekday
            DatePartEx = Weekday(newDate, FirstDayOfWeek)
        Case dtWeekdayName
            DatePartEx = Format(newDate, "dddd")
        Case dtShortWeekdayName
            DatePartEx = Format(newDate, "ddd")
        Case dtWeekOfTheYear
            DatePartEx = DatePart("ww", newDate, FirstDayOfWeek, _
                FirstWeekOfYear)
        Case dtHour
            DatePartEx = Hour(newDate)
        Case dtMinute
            DatePartEx = Minute(newDate)
        Case dtSecond
            DatePartEx = Second(newDate)
    End Select

End Function

Alberto Falossi
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date