In Visual Basic (VB.NET and VBA), the current date and time is obtained with the Now property, and you format it using the Format function, e.g., Format(Now, "yyyy-MM-dd HH:mm:ss"). This returns a string like “2025-04-02 14:30:00”. For date-only or time-only values, use Date and TimeOfDay respectively.
Understanding Now, Today, and TimeOfDay
VB provides three key properties for the current moment:
Now– returns both date and time as aDateTimevalue.Today– returns only the current date, with time set to midnight (00:00:00).TimeOfDay– returns only the current time, with date set to 1 January 0001.
You can also use Date.Now in VB.NET, which is identical to Now. In VBA (Excel, Access), Now is the standard. The choice affects formatting: if you use Today and try to display time, you’ll get 00:00:00.
The Format Function: Syntax and Common Patterns
The Format function is the primary tool for converting a DateTime to a formatted string. Its syntax is:
Format(expression, [format])
Where expression is a date/time value, and format is a string of format specifiers. If you omit the format, it uses the system’s short date and long time settings. Common format strings include:
"yyyy-MM-dd"– 2025-04-02 (ISO 8601)"dd/MM/yyyy"– 02/04/2025 (day first)"MM/dd/yyyy"– 04/02/2025 (month first)"HH:mm:ss"– 14:30:00 (24-hour)"hh:mm tt"– 02:30 PM (12-hour with AM/PM)"dddd, MMMM d, yyyy"– Wednesday, April 2, 2025
These specifiers are case-sensitive: MM is month, mm is minute. HH is 24-hour hour, hh is 12-hour hour.
Using ToString with Culture-Specific Formats
In VB.NET, you can also use the ToString method on a DateTime object, which offers more control over culture. For example:
Dim now As DateTime = DateTime.Now
Dim s As String = now.ToString("yyyy-MM-dd HH:mm:ss")
' Or with a culture:
Dim s2 As String = now.ToString("d", New CultureInfo("fr-FR")) ' 02/04/2025
The ToString method supports standard format strings like "d" (short date), "D" (long date), "t" (short time), "T" (long time), and "g" (general date/time). For custom formats, the same specifiers as Format apply.
Formatting in VBA (Excel, Access, Word)
In VBA, the Format function works exactly as described, but you often use it in cell formulas or VBA code. For example, in Excel VBA:
Dim myDate As Date
myDate = Now
Range("A1").Value = Format(myDate, "yyyy-mm-dd hh:nn:ss")
Note that VBA uses nn for minutes, not mm. This is a common mistake: mm in VBA means month. In VB.NET, mm is minutes. Always test your format string in the target environment.
Common Formatting Mistakes and How to Avoid Them
One frequent error is confusing month and minute specifiers. In VB.NET, MM is month, mm is minute. In VBA, mm is month, nn is minute. Another mistake is using single quotes in format strings – they are literal characters, not delimiters. For example, Format(Now, "'Date: 'yyyy") will include the word “Date:” literally.
Also, be aware that the system’s regional settings affect the default format. If you need a consistent format across machines, always specify an explicit format string or use a specific culture in ToString.
Alternatives to Format: Custom Formatting with String Interpolation
In VB.NET (15+), you can use string interpolation with format specifiers directly:
Dim s As String = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss}"
This is often more readable than the Format function. For VBA, there’s no interpolation, but you can use Format or concatenation. Another alternative is DateTime.ToString() with a culture, which is more flexible for international applications.
Frequently Asked Questions
How do I get just the current date in VB?
Use Date.Today in VB.NET or Date in VBA. For example, Format(Date.Today, "dd/MM/yyyy") gives the date without time.
What is the difference between Now and DateTime.Now?
They are identical in VB.NET. Now is a property of the Date module (VB-specific), while DateTime.Now is the .NET standard. Both return the current local date and time.
How do I convert a formatted string back to a DateTime?
Use DateTime.ParseExact or DateTime.TryParseExact in VB.NET, specifying the exact format. In VBA, use CDate or DateValue for simple conversions, but for custom formats you may need to parse manually.
Why does my format show “mm” as month instead of minutes?
Because you are likely in VBA, where mm is month. In VBA, use nn for minutes. In VB.NET, mm is minutes, so the same string works differently.
Can I use format strings in Excel formulas with NOW()?
Yes, but you use the TEXT function, not Format. For example, =TEXT(NOW(),"yyyy-mm-dd"). The format codes are similar but not identical; mm is minutes, hh is hours.
How do I display the time in 24-hour format?
Use HH:mm:ss (capital HH) in your format string. For example, Format(Now, "HH:mm") gives “14:30” instead of “02:30 PM”.
Related
- Shortcut for Insert? Keyboard Shortcuts Explained
- Free Expense Tracking Template Excel: Top Picks & Guide
- Random Number 1-30: Answer & How to Pick One
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- Goal Seek in Excel: A Practical Step-by-Step ExampleTo practice Goal Seek in Excel, create a formula that depends on one input, then choose Data >…
- Add a Page Break in Excel: Steps, Layout, and Print TipsTo add a page break in Excel for Microsoft 365 on Windows, select the cell where the next…
- How to Add Colour to Data Validation Cells in ExcelTo colour cells based on a data-validation dropdown in Excel, select the cells and use Home > Conditional…
- IFERROR in Google Sheets: Meaning and How to Use ItIn Google Sheets, IFERROR checks whether a formula produces an error and lets you show a different value…
- How to Sum in Excel: 5 Quick WaysTo sum numbers in Excel, the quickest way is to click an empty cell below or to the…
- Unmerge Excel Cells Without Losing DataTo unmerge cells in Excel for Microsoft 365 on Windows, select the merged cell or range, then choose…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.