Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

An enhanced VB DatePart function

Total Hit ( 4692)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


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.

Click here to copy the following block
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


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.