Skip to content

Tools - Date and Time Utilities

cToolsDateTime - Date and Time Tools

Overview

Provides Unix timestamp, ISO8601 format, and date formatting functionality.


Timestamp Retrieval

GetUnixTimestamp

Gets the Unix timestamp for the current time (13-digit milliseconds).

vb
Public Function GetUnixTimestamp() As Currency

Returns:

13-digit Unix timestamp (millisecond-level).

Example:

vb
Dim Ts As Currency
Ts = VBMAN.ToolsDateTime.GetUnixTimestamp()
Debug.Print Ts  ' Output: 1715904000000

GetIso8601Timestamp

Gets the ISO8601 formatted timestamp (UTC time).

vb
Public Function GetIso8601Timestamp() As String

Returns:

Format: YYYY-MM-DDTHH:MM:SSZ

Example:

vb
Dim Iso As String
Iso = VBMAN.ToolsDateTime.GetIso8601Timestamp()
Debug.Print Iso  ' Output: 2024-05-17T08:00:00Z

GetUtcTimestamp

Gets the UTC timestamp (Beijing time converted to UTC).

vb
Public Function GetUtcTimestamp() As String

Returns:

Format: YYYY-MM-DDTHH:MM:SSZ

Description:

  • Calculated based on current system time minus 8 hours
  • Suitable for scenarios requiring UTC time

Example:

vb
Dim Utc As String
Utc = VBMAN.ToolsDateTime.GetUtcTimestamp()
Debug.Print Utc  ' Output: 2024-05-17T08:00:00Z

Date Checking and Formatting

IsDatePast

Checks if a date has already passed.

vb
Public Function IsDatePast(targetDate As Variant) As Boolean

Parameters:

ParameterTypeDescription
targetDateVariantTarget date (string or date type)

Returns:

  • True - Target date has passed
  • False - Target date has not arrived
  • "Invalid Date Format" - Invalid date format

Example:

vb
' Check expiration
If VBMAN.ToolsDateTime.IsDatePast("2023-12-31") Then
    Debug.Print "Expired"
End If

' Check license
If VBMAN.ToolsDateTime.IsDatePast(LicenseExpiryDate) Then
    MsgBox "License has expired, please renew"
End If

FormatDateTime

Formats a date/time to the specified format.

vb
Public Function FormatDateTime(Optional ByVal strFormat As String = "yyyy-mm-dd hh:nn:ss", Optional ByVal varDate As Variant) As String

Parameters:

ParameterTypeDescription
strFormatStringFormat string (default "yyyy-mm-dd hh:nn:ss")
varDateVariantDate to format, uses current time if omitted

Common Formats:

FormatOutput Example
yyyy-mm-dd hh:nn:ss2024-05-17 14:30:00
yyyy年mm月dd日2024年05月17日
dd/mm/yyyy17/05/2024
hh:nn:ss AM/PM02:30:00 PM
yyyy-mm-dd2024-05-17

Example:

vb
' Current time
Debug.Print VBMAN.ToolsDateTime.FormatDateTime()  ' 2024-05-17 14:30:00

' Specific date
Debug.Print VBMAN.ToolsDateTime.FormatDateTime("yyyy年mm月dd日", "2024-5-17")
' Output: 2024年05月17日

' Format current time
Debug.Print VBMAN.ToolsDateTime.FormatDateTime("Today is yyyy年m月d日")
' Output: Today is 2024年5月17日

cTimeUse - Execution Time Measurement Tool

Overview

Simple code execution time measurement tool, based on GetTickCount API.

Methods

Start

Starts the timer.

vb
Public Sub Start()

Description:

  • Records current timestamp as start time
  • Call before the code you want to measure

Example:

vb
' Start timing
VBMAN.TimeUse.Start()

' Perform some operations
For i = 1 To 1000000
    ' Some calculation
Next i

' Show elapsed time
Debug.Print VBMAN.TimeUse.Show()

Show

Displays the elapsed time.

vb
Public Function Show(Optional Txt As String = "耗时:{*} 毫秒") As Variant

Parameters:

ParameterTypeDescription
TxtStringOutput format, * will be replaced with elapsed milliseconds

Returns:

Formatted string.

Example:

vb
' Start timing
VBMAN.TimeUse.Start()

' Perform some operations
For i = 1 To 1000000
    ' Some calculation
Next i

' Show elapsed time
Debug.Print VBMAN.TimeUse.Show()  ' Output: Elapsed: 50 milliseconds

' Custom format
Debug.Print VBMAN.TimeUse.Show("Operation completed, took {*} ms")  ' Output: Operation completed, took 50 ms
Debug.Print VBMAN.TimeUse.Show("Time: {*}ms")  ' Output: Time: 50ms

Complete Example

vb
Private Sub PerformanceTest()
    ' Test loop performance
    VBMAN.TimeUse.Start()
    
    Dim i As Long
    Dim Sum As Double
    For i = 1 To 1000000
        Sum = Sum + i
    Next i
    
    Debug.Print "Sum result: " & Sum
    Debug.Print VBMAN.TimeUse.Show("1 million loop iterations took {*} milliseconds")
    
    ' Test string concatenation
    VBMAN.TimeUse.Start()
    
    Dim str As String
    For i = 1 To 10000
        str = str & "x"
    Next i
    
    Debug.Print VBMAN.TimeUse.Show("String concatenation took {*} milliseconds")
    
    ' Test array operations
    VBMAN.TimeUse.Start()
    
    Dim Arr(1 To 10000) As String
    For i = 1 To 10000
        Arr(i) = "Item" & i
    Next i
    
    Debug.Print VBMAN.TimeUse.Show("Array operations took {*} milliseconds")
End Sub

cTimer / cTimers - Timer Classes

Overview

Provides high-precision timer functionality based on Windows API.

  • cTimer - Single timer
  • cTimers - Timer collection, supports multiple timers

cTimer Properties

PropertyTypeDescription
IntervalLongInterval time (milliseconds)
EnabledBooleanWhether enabled
TagVariantUser-defined data

cTimer Methods

EnableTimer

Enables the timer and sets the interval.

vb
Sub EnableTimer(mInterval As Long)

Enable / Disable

Enables/disables the timer.

vb
Sub Enable()
Sub Disable()

Reset

Resets the timer (restarts).

vb
Sub Reset()

cTimer Usage

vb
Private WithEvents Tmr As cTimer

Private Sub Form_Load()
    Set Tmr = New cTimer
    Tmr.Interval = 1000  ' 1 second
    Tmr.Enabled = True
End Sub

Private Sub Tmr_Timer()
    Debug.Print "Triggered once per second"
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Tmr.Enabled = False
    Set Tmr = Nothing
End Sub

cTimers Usage (Multiple Timers)

vb
Private WithEvents Timers As cTimers

Private Sub Form_Load()
    Set Timers = New cTimers
    
    ' Add two timers
    Timers.Add
    Timers.Add
    
    ' Configure first timer
    Timers(1).Interval = 1000
    Timers(1).Enabled = True
    
    ' Configure second timer
    Timers(2).Interval = 5000
    Timers(2).Enabled = True
End Sub

Private Sub Timers_Timer(ByVal Index As Integer)
    Debug.Print "Timer " & Index & " triggered"
    
    Select Case Index
        Case 1
            Debug.Print "Every-second task"
        Case 2
            Debug.Print "Every-5-seconds task"
    End Select
End Sub

Properties

PropertyDescription
IntervalInterval time (milliseconds)
EnabledWhether enabled
TagUser-defined data
IndexIndex in collection (for cTimers)

Complete Example

vb
Private Sub DateTimeDemo()
    ' ===== Timestamps =====
    
    ' Get Unix timestamp
    Dim UnixTs As Currency
    UnixTs = VBMAN.ToolsDateTime.GetUnixTimestamp()
    Debug.Print "Unix timestamp: " & UnixTs
    
    ' Get ISO8601 timestamp
    Dim IsoTime As String
    IsoTime = VBMAN.ToolsDateTime.GetIso8601Timestamp()
    Debug.Print "ISO8601: " & IsoTime
    
    ' ===== Date Formatting =====
    
    ' Various formats
    Debug.Print VBMAN.ToolsDateTime.FormatDateTime()
    Debug.Print VBMAN.ToolsDateTime.FormatDateTime("yyyy年mm月dd日")
    Debug.Print VBMAN.ToolsDateTime.FormatDateTime("hh:nn:ss")
    
    ' ===== Date Checking =====
    
    ' Check expiration
    Dim ExpiryDate As String
    ExpiryDate = "2024-12-31"
    
    If VBMAN.ToolsDateTime.IsDatePast(ExpiryDate) Then
        Debug.Print "Expired"
    Else
        Debug.Print "Not expired"
    End If
    
    ' ===== Performance Testing =====
    
    VBMAN.TimeUse.Start()
    
    ' Simulate time-consuming operation
    Dim i As Long
    For i = 1 To 100000
        DoEvents
    Next i
    
    Debug.Print VBMAN.TimeUse.Show("Operation completed, took {*} milliseconds")
End Sub

Use Cases

ScenarioMethod to Use
API RequestsGetUnixTimestamp() / GetIso8601Timestamp()
LoggingFormatDateTime()
License CheckingIsDatePast()
Performance TestingTimeUse.Start() + TimeUse.Show()
Scheduled TaskscTimer / cTimers
CountdownGetUnixTimestamp() comparison

VB6 and LOGO copyright of Microsoft Corporation