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).
Public Function GetUnixTimestamp() As CurrencyReturns:
13-digit Unix timestamp (millisecond-level).
Example:
Dim Ts As Currency
Ts = VBMAN.ToolsDateTime.GetUnixTimestamp()
Debug.Print Ts ' Output: 1715904000000GetIso8601Timestamp
Gets the ISO8601 formatted timestamp (UTC time).
Public Function GetIso8601Timestamp() As StringReturns:
Format: YYYY-MM-DDTHH:MM:SSZ
Example:
Dim Iso As String
Iso = VBMAN.ToolsDateTime.GetIso8601Timestamp()
Debug.Print Iso ' Output: 2024-05-17T08:00:00ZGetUtcTimestamp
Gets the UTC timestamp (Beijing time converted to UTC).
Public Function GetUtcTimestamp() As StringReturns:
Format: YYYY-MM-DDTHH:MM:SSZ
Description:
- Calculated based on current system time minus 8 hours
- Suitable for scenarios requiring UTC time
Example:
Dim Utc As String
Utc = VBMAN.ToolsDateTime.GetUtcTimestamp()
Debug.Print Utc ' Output: 2024-05-17T08:00:00ZDate Checking and Formatting
IsDatePast
Checks if a date has already passed.
Public Function IsDatePast(targetDate As Variant) As BooleanParameters:
| Parameter | Type | Description |
|---|---|---|
targetDate | Variant | Target date (string or date type) |
Returns:
True- Target date has passedFalse- Target date has not arrived"Invalid Date Format"- Invalid date format
Example:
' 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 IfFormatDateTime
Formats a date/time to the specified format.
Public Function FormatDateTime(Optional ByVal strFormat As String = "yyyy-mm-dd hh:nn:ss", Optional ByVal varDate As Variant) As StringParameters:
| Parameter | Type | Description |
|---|---|---|
strFormat | String | Format string (default "yyyy-mm-dd hh:nn:ss") |
varDate | Variant | Date to format, uses current time if omitted |
Common Formats:
| Format | Output Example |
|---|---|
yyyy-mm-dd hh:nn:ss | 2024-05-17 14:30:00 |
yyyy年mm月dd日 | 2024年05月17日 |
dd/mm/yyyy | 17/05/2024 |
hh:nn:ss AM/PM | 02:30:00 PM |
yyyy-mm-dd | 2024-05-17 |
Example:
' 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.
Public Sub Start()Description:
- Records current timestamp as start time
- Call before the code you want to measure
Example:
' 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.
Public Function Show(Optional Txt As String = "耗时:{*} 毫秒") As VariantParameters:
| Parameter | Type | Description |
|---|---|---|
Txt | String | Output format, * will be replaced with elapsed milliseconds |
Returns:
Formatted string.
Example:
' 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: 50msComplete Example
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 SubcTimer / cTimers - Timer Classes
Overview
Provides high-precision timer functionality based on Windows API.
cTimer- Single timercTimers- Timer collection, supports multiple timers
cTimer Properties
| Property | Type | Description |
|---|---|---|
Interval | Long | Interval time (milliseconds) |
Enabled | Boolean | Whether enabled |
Tag | Variant | User-defined data |
cTimer Methods
EnableTimer
Enables the timer and sets the interval.
Sub EnableTimer(mInterval As Long)Enable / Disable
Enables/disables the timer.
Sub Enable()
Sub Disable()Reset
Resets the timer (restarts).
Sub Reset()cTimer Usage
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 SubcTimers Usage (Multiple Timers)
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 SubProperties
| Property | Description |
|---|---|
Interval | Interval time (milliseconds) |
Enabled | Whether enabled |
Tag | User-defined data |
Index | Index in collection (for cTimers) |
Complete Example
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 SubUse Cases
| Scenario | Method to Use |
|---|---|
| API Requests | GetUnixTimestamp() / GetIso8601Timestamp() |
| Logging | FormatDateTime() |
| License Checking | IsDatePast() |
| Performance Testing | TimeUse.Start() + TimeUse.Show() |
| Scheduled Tasks | cTimer / cTimers |
| Countdown | GetUnixTimestamp() comparison |