VB6Parse / Library / Datetime / date_dollar

VB6 Library Reference

Date$ Function

Returns the current system date as a String. The dollar sign suffix ($) explicitly indicates that this function returns a String type (not a Variant).

Syntax

Date$

Parameters

None. The Date$ function takes no parameters.

Return Value

Returns a String containing the current system date. The format depends on the system's regional settings (typically "mm/dd/yyyy" in US or "dd/mm/yyyy" in other regions). The return value is always a String type (never Variant).

Remarks

Typical Uses

  1. Date stamping - Add date stamps to log entries, files, or records
  2. Display formatting - Show current date to users
  3. File naming - Include date in filenames
  4. Logging - Record when events occurred
  5. Report generation - Add date headers to reports
  6. Audit trails - Track when data was created or modified
  7. String concatenation - Combine date with other text

Basic Examples

' Example 1: Get current date as string
Dim dateStr As String
dateStr = Date$
' Example 2: Display current date
MsgBox "Today is: " & Date$
' Example 3: Create date stamp
Dim stamp As String
stamp = "Report generated on " & Date$
' Example 4: Simple assignment
currentDate = Date$

Common Patterns

File Naming with Date

Function CreateDateStampedFilename(baseName As String) As String
Dim dateStr As String
Dim cleanDate As String

' Get date and remove slashes
dateStr = Date$
cleanDate = Replace$(dateStr, "/", "")

CreateDateStampedFilename = baseName & "_" & cleanDate & ".txt"
End Function

Log Entry with Date

Sub WriteLogEntry(message As String)
Dim logFile As Integer
Dim logEntry As String

logFile = FreeFile
Open "application.log" For Append As #logFile

logEntry = Date$ & " - " & message
Print #logFile, logEntry

Close #logFile
End Sub

Date-Based Conditional Logic

Sub CheckDate()
Dim todayStr As String
todayStr = Date$

' Simple string comparison (locale-dependent)
If todayStr = "12/25/2025" Then
MsgBox "Merry Christmas!"
End If
End Sub

Report Header

Function CreateReportHeader(title As String) As String
Dim header As String
header = String$(60, "=") & vbCrLf
header = header & title & vbCrLf
header = header & "Generated: " & Date$ & vbCrLf
header = header & String$(60, "=") & vbCrLf
CreateReportHeader = header
End Function

Date Display in Status Bar

Sub UpdateStatusBar()
Form1.StatusBar.Panels(1).Text = "Date: " & Date$
End Sub

Backup File Naming

Function GetBackupFilename(originalFile As String) As String
Dim baseName As String
Dim extension As String
Dim dotPos As Integer
Dim dateStr As String

dotPos = InStrRev(originalFile, ".")
If dotPos > 0 Then
baseName = Left$(originalFile, dotPos - 1)
extension = Mid$(originalFile, dotPos)
Else
baseName = originalFile
extension = ""
End If

' Clean date string for filename
dateStr = Replace$(Date$, "/", "-")

GetBackupFilename = baseName & "_backup_" & dateStr & extension
End Function

Daily Log File

Function GetDailyLogFilename() As String
Dim dateStr As String
dateStr = Replace$(Date$, "/", "")
GetDailyLogFilename = "log_" & dateStr & ".txt"
End Function

Date Validation (Simple)

Function IsToday(dateStr As String) As Boolean
IsToday = (dateStr = Date$)
End Function

Combining Date and Time

Function GetDateTimeStamp() As String
GetDateTimeStamp = Date$ & " " & Time$
End Function

Data Export Header

Sub ExportData()
Dim exportFile As Integer

exportFile = FreeFile
Open "export.csv" For Output As #exportFile

' Write header with date
Print #exportFile, "Data Export - " & Date$
Print #exportFile, "Name,Value,Status"

' Export data...

Close #exportFile
End Sub

Best Practices

  1. Use Format$ instead of Date$ when you need specific date formats
  2. Be aware that Date$ format depends on system locale settings
  3. For file naming, clean the date string (remove or replace slashes)
  4. Use Date$ instead of Date when you need a string result
  5. For date comparisons, use Date (Variant) instead of Date$ (String)
  6. Don't assume a specific date format - it varies by locale
  7. For consistent formatting, use Format$(Date, "yyyy-mm-dd")
  8. Test with different regional settings if your app is international
  9. Store dates in consistent format (ISO 8601 recommended)
  10. Use DateValue to parse date strings reliably

Performance Considerations

Locale Considerations

The format of Date$ varies by system locale:

Locale Example Format Sample Output
US (English) mm/dd/yyyy "12/25/2025"
UK (English) dd/mm/yyyy "25/12/2025"
Germany dd.mm.yyyy "25.12.2025"
Japan yyyy/mm/dd "2025/12/25"
France dd/mm/yyyy "25/12/2025"

Common Pitfalls

  1. String Comparison: Comparing Date$ strings directly is locale-dependent and unreliable
' BAD - locale-dependent
If Date$ = "12/25/2025" Then

' GOOD - use Date variants
If Date = #12/25/2025# Then
  1. Date Parsing: Don't parse Date$ manually - use DateValue instead
' BAD - fragile parsing
parts = Split(Date$, "/")

' GOOD - use built-in functions
currentYear = Year(Date)
currentMonth = Month(Date)
  1. Filename Safety: Date strings may contain invalid filename characters
' BAD - slashes invalid in filenames
filename = "report_" & Date$ & ".txt"

' GOOD - replace invalid characters
filename = "report_" & Replace$(Date$, "/", "-") & ".txt"

Limitations

← Back to Datetime | View all functions