|
An enhanced VB DatePart function
|
Total Hit (4692) |
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.
|
Rating
|
|
|
Benchmarks with millisecond accuracy
|
Total Hit (3883) |
The Timer function returns a value which is only accurate to about 55 milliseconds, therefore it is not very useful for doing accurate benchmarks. If you need a better resolution you may try out this function:
|
Rating
|
|
|
Beware of slashes when formatting dates
|
Total Hit (2945) |
You probably use the Format function to format date, for example:
«Code LangId=1»
? Format(Now, "mm/dd/yyyy") ' => 08/01/2001
? Format(Now, "mm+dd+yyyy") ' => 08+01+2001
«/Code»
It seems that this is everything you need to know, right? However, the story is quite different
....Read More |
Rating
|
|
|
Create a system timer using AddressOf and a callback function
|
Total Hit (4106) |
The Timer control is great when you want to periodically execute a piece of code while the program is doing something else. However, it also has a couple of shortcomings: (1) it requires a parent form, so you can't use it directly inside a BAS module, and (2) it's Interval property can't be higher t
....Read More |
Rating
|
|
|
Evaluate the number of days remaining in the current year
|
Total Hit (2935) |
The DatePart() function can return the number of days from the beginning of the current year, but there is no VB built-in function that returns the number of days left to the end of the year. You can work around this with the DateDiff function:
«Code LangId=1»
' aDate contains a valid date
Days
....Read More |
Rating
|
|
|
High-precision timing with the QueryPerformanceCounter API function
|
Total Hit (4891) |
While VB Timer functions is sufficiently precise for most tasks, it doesn't provide the highest possible resolution most modern computer can provide. If your hardware supports high-resolution counters, you can do a much better job with a pair of API functions:
«Code LangId=1»
Private Type LARGE_
....Read More |
Rating
|
|
|
Retrieve Time Zone information
|
Total Hit (7157) |
The GetTimeZoneInformation API returns a TIME_ZONE_INFORMATION variable that contains several pieces of information about system's time date and time setting. The first Long value in this structure holds the "distance" (in minutes) from Greenwich standard time, so it's quite easy to create a GetTime
....Read More |
Rating
|
|
|
Test for Leap Year
|
Total Hit (3305) |
As long as you trust VB's IsDate function (and you can, trust me), here's the shorted and fastest method to check if a year is a leap year or not:
«Code LangId=1»
Function IsLeapYear(year As Integer) As Boolean
IsLeapYear = IsDate("2/29/" & CStr(year))
End Function
«/Code»
Here's is anot
....Read More |
Rating
|
|
|
The age of a person
|
Total Hit (2852) |
You can quickly evaluate the age of a person using the following function:
«Code LangId=1»
Function Age(BirthDate As Date, Optional CurrentDate As Variant) As Integer
If IsMissing(CurrentDate) Then CurrentDate = Now
Age = Year(CurrentDate) - Year(BirthDate)
End Property
«/Code»
If
....Read More |
Rating
|
|
|
The beginning or end of previous week
|
Total Hit (3813) |
For reporting, many times you need to figure out what date last week began and ended. Use the code below to figure that out:
|
Rating
|
|
|
The number of days in a month
|
Total Hit (3868) |
You need just one-line function to evaluate the number of days in a month (while taking leap years into account, of course). The trick is to use the DateDiff and DateSerial functions to calculate the difference between the first day of the current month and the first day of the subsequent month :
....Read More |
Rating
|
|
|
Tricks with DateSerial
|
Total Hit (3228) |
The DateSerial function has an interesting feature: it doesn't raise errors when you pass it an invalid month or day number. Instead, it evaluates the date as if the arguments were valid. For example, DateSerial(2000, 1, 32) returns the Date value of February 1, 2000. This behavior can (and should)
....Read More |
Rating
|
|
|
Use Sleep API to add pauses to your programs
|
Total Hit (4043) |
Don't use an empty For...Next loop to add pauses to your programs; instead, use the Sleep API function, that releases the CPU and lets other apps in the system effectively multitask:
«Code LangId=1»
Declare Sub Sleep Lib "kernel32" (ByVal milliseconds As Long)
' pause for 5 seconds
Sleep 5000
....Read More |
Rating
|
|
|
|
|
|
|
|
GetDBDate - Formatting a date as a DateSerial for Access
|
Total Hit (2903) |
«Code LangId=1»' Format a date as a DateSerial for Access
' Example: Debug.Print GetDBDate(date)
Public Function GetDBDate(sDate As String) As String
On Error GoTo ERROR_GetDBDate
Dim sTmp As String
If IsDate(sDate) = False Then
sTmp = sDate
Else
sTmp = "DateSe
....Read More |
Rating
|
|
|
GetRoshHashanah - Get the date of Rosh Hashanah (Jewish calendar)
|
Total Hit (2900) |
«Code LangId=1»' Returns the date that Rosh Hashanah begins
' for the requested year. It is important to note that
' Rosh Hashanah is based on the Lunar cycle so the Holiday
' actually begins at sunset of the day before the date
' returned by this function.
' Note: Many dates in the Jewish
....Read More |
Rating
|
|
|
IsValidDateField - Check whether a date is valid
|
Total Hit (3455) |
«Code LangId=1»Enum psDateTypes
AnyValidDate 'Allows any valid date to be entered
PastDate 'Only allows past dates (before today) to be entered
FutureDate 'Only allows future dates (after today) to be entered
TodayOrFuture 'Only allows today or future date to be
....Read More |
Rating
|
|
|
Monday - retrieving the date of the Monday for a specified week
|
Total Hit (2793) |
«Code LangId=1»
' Return the date of the Monday for a specified week..
' This function can be tweaked to return any weekday. I use it in Access to
' subdivide reports into weekly units, since Access displays only a number
' between 1 and 53 for the week when you group dates by week.
'
' Note
....Read More |
Rating
|
|
|
|
|
|
|
|
TimeToString - Convert time to a descriptive string
|
Total Hit (3869) |
«Code LangId=1»' convert a date value into a string in the format
' YY years, MM months, DD days, HH hours, MM minutes, SS.HH seconds)
' you can also opt for time short format (HH h, MM m, SS s)
Function TimeToString(ByVal aDate As Date, Optional ShortTimeFormat As Boolean, _
Optional
....Read More |
Rating
|
|
|
|
How to retrive and change system's short/long date format
|
Total Hit (7950) |
This article will demonstrate how to retrive system's local name and short/long date format. It will also show you how to change current short/long date format of the system.
To implement Copy/Paste Demo
- Create a standard exe project
- Add a module
- Place 3 labels on the form1 (to diaplay
....Read More |
Rating
|
|