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
- The
Date$function always returns aString, whileDate(without$) returns aVariantof subtypeDate. - Returns only the date portion (no time information).
- Uses system date from computer's clock.
- Date format depends on system locale/regional settings.
- Common formats: "mm/dd/yyyy" (US), "dd/mm/yyyy" (Europe), "yyyy/mm/dd" (ISO).
- The string representation may include leading zeros (e.g., "01/05/2025").
- For better performance when you need a string, use
Date$instead ofDate. - Cannot be used to set the system date (unlike
Datestatement).
Typical Uses
- Date stamping - Add date stamps to log entries, files, or records
- Display formatting - Show current date to users
- File naming - Include date in filenames
- Logging - Record when events occurred
- Report generation - Add date headers to reports
- Audit trails - Track when data was created or modified
- 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
Related Functions
Date: Returns current date asVariantinstead ofStringNow: Returns current date and timeTime$: Returns current time asStringFormat$: Formats dates with custom patternsYear: Extracts year from dateMonth: Extracts month from dateDay: Extracts day from dateDateSerial: Creates date from year, month, dayDateValue: Converts string to date
Best Practices
- Use
Format$instead ofDate$when you need specific date formats - Be aware that
Date$format depends on system locale settings - For file naming, clean the date string (remove or replace slashes)
- Use
Date$instead ofDatewhen you need a string result - For date comparisons, use
Date(Variant) instead ofDate$(String) - Don't assume a specific date format - it varies by locale
- For consistent formatting, use
Format$(Date, "yyyy-mm-dd") - Test with different regional settings if your app is international
- Store dates in consistent format (ISO 8601 recommended)
- Use
DateValueto parse date strings reliably
Performance Considerations
Date$is slightly more efficient thanDatewhen you need a string- System date/time calls are fast but not free
- Cache the result if you need it multiple times in quick succession
- For high-frequency logging, consider caching the date string
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
- 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
- Date Parsing: Don't parse
Date$manually - useDateValueinstead
' BAD - fragile parsing
parts = Split(Date$, "/")
' GOOD - use built-in functions
currentYear = Year(Date)
currentMonth = Month(Date)
- 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
- Cannot be used to set the system date (use
Datestatement for that) - Format is system-dependent and cannot be directly controlled
- No time information included (use
NoworTime$for time) - String comparison of dates is unreliable across locales
- Cannot specify date format (use
Format$for custom formats)