VB6Parse / Library / Datetime / time

VB6 Library Reference

VB6 Time Function

The Time function returns a Variant (Date) indicating the current system time.

Syntax

Time()

or

Time

Parameters

None. The Time function takes no arguments.

Returns

Returns a Variant of subtype Date containing the current system time. The date portion is set to zero (December 30, 1899).

Remarks

The Time function retrieves the current system time:

Time Components

Extract time components using:

currentHour = Hour(Time)      ' 0-23
currentMinute = Minute(Time)  ' 0-59
currentSecond = Second(Time)  ' 0-59

Time Arithmetic

' Add 1 hour to current time
newTime = Time + TimeSerial(1, 0, 0)

' Add 30 minutes to current time
newTime = DateAdd("n", 30, Time)

Typical Uses

  1. Timestamp Logging: Record when events occur
  2. Time-based Triggers: Check current time for scheduled operations
  3. Time Display: Show current time in user interface
  4. Performance Timing: Measure operation duration (though Timer is better)
  5. Time Validation: Check if operation is within allowed time window
  6. Time Calculations: Calculate time differences or future times
  7. Scheduling: Determine if tasks should run now
  8. Time Formatting: Create custom time displays

Basic Examples

Example 1: Display Current Time

Sub ShowCurrentTime()
MsgBox "Current time is: " & Time
End Sub

Example 2: Log Event Time

Sub LogEvent(eventName As String)
Dim logEntry As String
logEntry = Time & " - " & eventName
Debug.Print logEntry
End Sub

Example 3: Check Business Hours

Function IsBusinessHours() As Boolean
Dim currentHour As Integer
currentHour = Hour(Time)
IsBusinessHours = (currentHour >= 9 And currentHour < 17)
End Function

Example 4: Format Time Display

Function GetFormattedTime() As String
GetFormattedTime = Format$(Time, "hh:mm:ss AM/PM")
End Function

Common Patterns

Pattern 1: Get Current Hour

Function GetCurrentHour() As Integer
GetCurrentHour = Hour(Time)
End Function

Pattern 2: Time-based Greeting

Function GetGreeting() As String
Dim currentHour As Integer
currentHour = Hour(Time)

Select Case currentHour
Case 0 To 11
GetGreeting = "Good morning"
Case 12 To 17
GetGreeting = "Good afternoon"
Case Else
GetGreeting = "Good evening"
End Select
End Function

Pattern 3: Create Timestamp

Function CreateTimestamp() As String
CreateTimestamp = Format$(Date, "yyyy-mm-dd") & " " & Format$(Time, "hh:nn:ss")
End Function

Pattern 4: Check Time Window

Function IsWithinTimeWindow(startTime As Date, endTime As Date) As Boolean
Dim currentTime As Date
currentTime = Time
IsWithinTimeWindow = (currentTime >= startTime And currentTime <= endTime)
End Function

Pattern 5: Add Time Duration

Function AddMinutes(minutes As Integer) As Date
AddMinutes = DateAdd("n", minutes, Time)
End Function

Pattern 6: Time Until Target

Function MinutesUntil(targetTime As Date) As Long
MinutesUntil = DateDiff("n", Time, targetTime)
End Function

Pattern 7: Round Time to Nearest Interval

Function RoundToNearest15Minutes() As Date
Dim currentTime As Date
Dim minutes As Integer

currentTime = Time
minutes = Minute(currentTime)
minutes = ((minutes + 7) \ 15) * 15

RoundToNearest15Minutes = TimeSerial(Hour(currentTime), minutes, 0)
End Function

Pattern 8: Compare Times

Function IsTimeBefore(compareTime As Date) As Boolean
IsTimeBefore = (Time < compareTime)
End Function

Pattern 9: Get Time String

Function GetTimeString() As String
GetTimeString = Format$(Time, "hh:mm:ss")
End Function

Pattern 10: Calculate Elapsed Time

Function GetElapsedMinutes(startTime As Date) As Long
GetElapsedMinutes = DateDiff("n", startTime, Time)
End Function

Advanced Usage

Example 1: Time Tracker Class

' Class: TimeTracker
' Tracks operation start/end times and durations
Option Explicit

Private m_StartTime As Date
Private m_EndTime As Date
Private m_IsRunning As Boolean

Public Sub StartTracking()
m_StartTime = Time
m_IsRunning = True
End Sub

Public Sub StopTracking()
If Not m_IsRunning Then
Err.Raise 5, , "Tracking not started"
End If

m_EndTime = Time
m_IsRunning = False
End Sub

Public Function GetElapsedMinutes() As Long
If m_IsRunning Then
GetElapsedMinutes = DateDiff("n", m_StartTime, Time)
Else
GetElapsedMinutes = DateDiff("n", m_StartTime, m_EndTime)
End If
End Function

Public Function GetElapsedSeconds() As Long
If m_IsRunning Then
GetElapsedSeconds = DateDiff("s", m_StartTime, Time)
Else
GetElapsedSeconds = DateDiff("s", m_StartTime, m_EndTime)
End If
End Function

Public Function GetFormattedDuration() As String
Dim totalSeconds As Long
Dim hours As Long
Dim minutes As Long
Dim seconds As Long

totalSeconds = GetElapsedSeconds()
hours = totalSeconds \ 3600
minutes = (totalSeconds Mod 3600) \ 60
seconds = totalSeconds Mod 60

GetFormattedDuration = Format$(hours, "00") & ":" & _
Format$(minutes, "00") & ":" & _
Format$(seconds, "00")
End Function

Public Property Get IsRunning() As Boolean
IsRunning = m_IsRunning
End Property

Example 2: Schedule Manager Module

' Module: ScheduleManager
' Manages time-based scheduling and windows
Option Explicit

Public Function IsWithinSchedule(scheduleStart As Date, scheduleEnd As Date) As Boolean
Dim currentTime As Date
currentTime = Time

' Handle overnight schedules (e.g., 10 PM to 6 AM)
If scheduleStart > scheduleEnd Then
IsWithinSchedule = (currentTime >= scheduleStart Or currentTime <= scheduleEnd)
Else
IsWithinSchedule = (currentTime >= scheduleStart And currentTime <= scheduleEnd)
End If
End Function

Public Function GetNextScheduledTime(targetTime As Date) As Date
Dim currentTime As Date
currentTime = Time

If currentTime < targetTime Then
' Target time is later today
GetNextScheduledTime = Date + targetTime
Else
' Target time is tomorrow
GetNextScheduledTime = Date + 1 + targetTime
End If
End Function

Public Function MinutesUntilSchedule(scheduleTime As Date) As Long
Dim currentTime As Date
Dim targetDateTime As Date

currentTime = Time

If currentTime < scheduleTime Then
' Later today
targetDateTime = Date + scheduleTime
Else
' Tomorrow
targetDateTime = Date + 1 + scheduleTime
End If

MinutesUntilSchedule = DateDiff("n", Now, targetDateTime)
End Function

Public Function FormatTimeRemaining(targetTime As Date) As String
Dim minutesLeft As Long
Dim hours As Long
Dim minutes As Long

minutesLeft = MinutesUntilSchedule(targetTime)

If minutesLeft < 0 Then
FormatTimeRemaining = "Overdue"
Exit Function
End If

hours = minutesLeft \ 60
minutes = minutesLeft Mod 60

If hours > 0 Then
FormatTimeRemaining = hours & "h " & minutes & "m remaining"
Else
FormatTimeRemaining = minutes & "m remaining"
End If
End Function

Example 3: Time Logger Class

' Class: TimeLogger
' Logs timestamped events
Option Explicit

Private m_LogEntries As Collection

Private Sub Class_Initialize()
Set m_LogEntries = New Collection
End Sub

Public Sub LogEvent(eventDescription As String)
Dim logEntry As String
logEntry = Format$(Time, "hh:mm:ss") & " - " & eventDescription
m_LogEntries.Add logEntry
End Sub

Public Sub LogEventWithDetails(eventName As String, details As String)
Dim logEntry As String
logEntry = Format$(Time, "hh:mm:ss") & " - " & eventName & ": " & details
m_LogEntries.Add logEntry
End Sub

Public Function GetLogEntries() As String
Dim result As String
Dim entry As Variant

result = "Event Log:" & vbCrLf
result = result & String$(50, "=") & vbCrLf

For Each entry In m_LogEntries
result = result & entry & vbCrLf
Next entry

GetLogEntries = result
End Function

Public Sub ClearLog()
Set m_LogEntries = New Collection
End Sub

Public Property Get EntryCount() As Long
EntryCount = m_LogEntries.Count
End Property

Public Function ExportToFile(filename As String) As Boolean
On Error GoTo ErrorHandler

Dim fileNum As Integer
Dim entry As Variant

fileNum = FreeFile
Open filename For Output As #fileNum

Print #fileNum, "Event Log - " & Format$(Date, "yyyy-mm-dd")
Print #fileNum, String$(50, "=")

For Each entry In m_LogEntries
Print #fileNum, entry
Next entry

Close #fileNum
ExportToFile = True
Exit Function

ErrorHandler:
ExportToFile = False
If fileNum > 0 Then Close #fileNum
End Function

Example 4: Business Hours Validator Module

' Module: BusinessHoursValidator
' Validates and manages business hours
Option Explicit

Private m_BusinessStart As Date
Private m_BusinessEnd As Date
Private m_LunchStart As Date
Private m_LunchEnd As Date

Public Sub Initialize(businessStart As Date, businessEnd As Date, _
Optional lunchStart As Variant, Optional lunchEnd As Variant)
m_BusinessStart = businessStart
m_BusinessEnd = businessEnd

If Not IsMissing(lunchStart) Then m_LunchStart = lunchStart
If Not IsMissing(lunchEnd) Then m_LunchEnd = lunchEnd
End Sub

Public Function IsBusinessHours() As Boolean
Dim currentTime As Date
currentTime = Time

' Check if within business hours
If currentTime < m_BusinessStart Or currentTime >= m_BusinessEnd Then
IsBusinessHours = False
Exit Function
End If

' Check if during lunch (if configured)
If m_LunchStart > 0 And m_LunchEnd > 0 Then
If currentTime >= m_LunchStart And currentTime < m_LunchEnd Then
IsBusinessHours = False
Exit Function
End If
End If

IsBusinessHours = True
End Function

Public Function GetBusinessHoursStatus() As String
Dim currentTime As Date
currentTime = Time

If currentTime < m_BusinessStart Then
GetBusinessHoursStatus = "Before business hours (opens at " & _
Format$(m_BusinessStart, "h:mm AM/PM") & ")"
ElseIf currentTime >= m_BusinessEnd Then
GetBusinessHoursStatus = "After business hours (closed at " & _
Format$(m_BusinessEnd, "h:mm AM/PM") & ")"
ElseIf m_LunchStart > 0 And currentTime >= m_LunchStart And currentTime < m_LunchEnd Then
GetBusinessHoursStatus = "Lunch break (returns at " & _
Format$(m_LunchEnd, "h:mm AM/PM") & ")"
Else
GetBusinessHoursStatus = "Open for business"
End If
End Function

Public Function MinutesUntilOpen() As Long
Dim currentTime As Date
currentTime = Time

If currentTime < m_BusinessStart Then
MinutesUntilOpen = DateDiff("n", currentTime, m_BusinessStart)
Else
' Next business day
Dim nextOpen As Date
nextOpen = DateAdd("d", 1, Date) + m_BusinessStart
MinutesUntilOpen = DateDiff("n", Now, nextOpen)
End If
End Function

Public Function MinutesUntilClose() As Long
Dim currentTime As Date
currentTime = Time

If currentTime >= m_BusinessEnd Then
MinutesUntilClose = 0
Else
MinutesUntilClose = DateDiff("n", currentTime, m_BusinessEnd)
End If
End Function

Error Handling

The Time function typically does not raise errors under normal circumstances:

Performance Notes

Best Practices

  1. Use Now for timestamps if you need both date and time
  2. Use Timer for performance measurements (higher precision)
  3. Cache Time value if using multiple times in tight loop
  4. Use Format$ to display time in specific format
  5. Consider time zones for distributed applications
  6. Validate system time is set correctly if critical
  7. Use TimeSerial to create specific time values for comparison
  8. Handle overnight periods carefully (when start > end time)
  9. Store as Date type not String for calculations
  10. Document time format assumptions in code comments

Comparison Table

Function Returns Includes Date Includes Time Precision
Time Variant (Date) No (zero date) Yes 1 second
Date Variant (Date) Yes No (midnight) 1 day
Now Variant (Date) Yes Yes 1 second
Timer Single No Yes (seconds since midnight) High
Time$ String No Yes 1 second

Platform Notes

Limitations

← Back to Datetime | View all functions