When I format the date so that a field gets the value as year-month-day-hour-minute-second, I omly see the date-portion, the rest is filled with zeros.
I use:
strDT = Format(Date, "yyyymmddhhmmss")
I get '20140226000000'
What goes wrong, or how can I get the desired result?

Dani AI

Generated

Nice catch by — the formatted string will only show nonzero time fields if the value you format actually contains a time component. Two common causes for seeing zeros in the hhmmss part are: (1) the Date/Date-only value has its time set to midnight, and (2) using the wrong format tokens for the VB flavor you’re on. The tokens differ between classic VB/VBA and VB.NET, so the same picture string can mean different things.

For VB.NET prefer DateTime with a clear custom format and an invariant culture so results are stable across locales:

Dim stamp As String = DateTime.Now.ToString("yyyyMMddHHmmss", System.Globalization.CultureInfo.InvariantCulture)

(Explanation: yyyy = 4-digit year, MM = month, dd = day, HH = 24-hour hour, mm = minute, ss = second. In .NET case matters: M is month, m is minute.)

For VBA/VB6 use the Format function with a Date/Time value (Format patterns there are similar but the interpretation can be context-dependent), for example:

Dim stamp As String
stamp = Format$(Now(), "yyyyMMddHHmmss")

Practical tips: confirm the source value actually contains a nonzero time (inspect it in the debugger or print Hour(...), Minute(...), Second(...)), prefer DateTime/UTC for logs that cross time zones, and avoid locale-sensitive formats if you need a machine-sortable timestamp (use invariant culture or build the string from numeric parts). Microsoft docs for .NET custom date/time patterns and the VBA Format function provide exact token rules and examples:
Custom Date and Time Format Strings

Recommended Answers

All 2 Replies

Hi hvebsr welcome to DaniWeb.
Probably means your date variable does not store hour,minute and second info.

Hello ddanbe,
I resolved it by using 'now' in stead of 'date'
Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.