Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Desktop Developer

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

Search

XML Feeds

Google Ads

Category: Visual Basic 5 & 6

comments
Rate Post:
submit to reddit Digg!FacebookDotnetkicks

Have you ever needed to remove the time part of a date variable, or remove the date part of a date variable? I recently had a requirement to do this, and my first reaction was to use the format function because it is very flexible and quite simple to use. Unfortunately, it does't perform very well.

First, the format method for removing the time part.

Debug.Print Format(Now, "Short Date")
4/1/2009

Debug.Print Format(Now, "Short Time")
09:16 

Seems simple enough to remove the date and/or time portion of a date variable, but is it the best way? If you only want to display it, it probably is, but if you need to use the values for calculations, it is not the best way to remove the date or time portions.

Instead, you can use the Int function.

The Int function performs a simple truncate on a number. Note: this does not do rounding. Also significant is that Int will return whatever data is sent to it. If the parameter is a double, int will return a double. If the parameter is a single, Int returns a single, if the parameter is a date, Int returns a date.

Debug.Print Int(Now)
4/1/2009 

Debug.Print Now - Int(Now)
0.390729166669189  

Surprised to see a number instead of a time? Me too. But... in VB6, when you subtract dates, the resulting data type is a double.

Debug.Print TypeName(Now - Int(Now))
Double 

However, this double can be converted back to a Date variable type like this.

Debug.Print cDate(Now - Int(Now))
9:25:40 AM 

Clearly, there are 2 different methods for separating the date from the time in a Date variable. Which is better? They both return the correct value, so now it's up to performance.

I tested the performance with this code:

  1. Option Explicit
  2.  
  3. Private Sub Command1_Click()
  4.  
  5.     Dim i As Long
  6.     Dim dteTemp As Date
  7.     Dim Start As Single
  8.    
  9.     Start = Timer
  10.     For i = 1 To 1000000
  11.         dteTemp = RemoveDate(Now)
  12.     Next
  13.     Call MsgBox(Format(Timer - Start, "0.00") & " micro-seconds")
  14.    
  15.     Start = Timer
  16.     For i = 1 To 1000000
  17.         dteTemp = RemoveDateWithFormat(Now)
  18.     Next
  19.     Call MsgBox(Format(Timer - Start, "0.00") & " micro-seconds")
  20.    
  21. End Sub
  22.  
  23. Private Function RemoveDateWithFormat(ByVal DateTime As Date) As Date
  24.    
  25.     RemoveDateWithFormat = Format(DateTime, "h:mm")
  26.  
  27. End Function
  28.  
  29. Private Function RemoveDate(ByVal DateTime As Date) As Date
  30.    
  31.     RemoveDate = DateTime - Int(DateTime)
  32.    
  33. End Function

The Int method runs in 0.40 micro-seconds and the Format method takes 3 micro-seconds. The Int method is approximately 7 times faster.

About the Author

George has been developing software professionally for 19 years, first for the department of defense, and then for various other companies. In 1998, George started his software company, Orbit Software, specializing in School Bus Transportation software. His specialty is refining SQL Server queries to deliver optimal performance.
Social SitingsTwitterLTD RSS Feed
1982 views
submit to reddit Digg!FacebookDotnetkicks

Comments and Feedback