Formatting dates and time
The Format function can also be used on Date/Time serial numbers to extract and format all kinds of information ranging from seconds to days to years. There are a handful of named format strings and constructing your own strings are not out of the question. No one except Bill usually memorizes the formatting characters, so I'll just dump a list of them on you here.
Formatting numbers as date/time expressions |
Expression |
Result |
Format(36715.5784, "general date") |
7/8/00 1:52:54 PM |
Format(36715.5784, "short date") |
7/8/00 |
Format(36715.5784, "medium date") |
08-Jul-00 |
Format(36715.5784, "long date") |
Saturday, July 08, 2000 |
Format(36715.5784, "short time") |
13:52 |
Format(36715.5784, "medium time") |
01:52 PM |
Format(36715.5784, "long time") |
1:52:54 PM |
Format(36715.5784, "c") |
7/8/00 1:52:54 PM |
Format(36715.5784, "d") |
8 |
Format(36715.5784, "dd") |
08 |
Format(36715.5784, "ddd") |
Sat |
Format(36715.5784, "dddd") |
Saturday |
Format(36715.5784, "ddddd") |
7/8/00 |
Format(36715.5784, "dddddd") |
Saturday, July 08, 2000 |
Format(36715.5784, "w") |
7 |
Format(36715.5784, "ww") |
28 |
Format(36715.5784, "m") |
7 |
Format(36715.5784, "mm") |
07 |
Format(36715.5784, "mmm") |
Jul |
Format(36715.5784, "mmmm") |
July |
Format(36715.5784, "q") |
3 |
Format(36715.5784, "y") |
190 |
Format(36715.5784, "yy") |
00 |
Format(36715.5784, "yyyy") |
2000 |
Format(36715.5784, "h") |
13 |
Format(36715.5784, "hh") |
13 |
Format(36715.5784, "n") |
52 |
Format(36715.5784, "nn") |
52 |
Format(36715.5784, "s") |
54 |
Format(36715.5784, "ss") |
54 |
Format(36715.5784, "ttttt") |
1:52:54 PM |
Format(36715.5784, "AM/PM") |
PM |
Format(36715.5784, "am/pm") |
pm |
Format(36715.5784, "A/P") |
P |
Format(36715.5784, "a/p") |
p |
Format(36715.5784, "AMPM") |
PM |
Notes: Some of this formatting is duplicated by easier-to-use Date and Time functions. Results of the named formats (short date, medium date, etc.) may vary depending on Control Panel Regional Settings.
-
Format "w" returns day of week (1 = Sunday, 7 = Saturday)
-
Format "ww" returns week of year (1-53)
-
Format "y" returns day of year (1-366)
-
Format "h" returns hour of day as one or two digits...if necessary
-
Format "hh" returns hour of day as two digits...definitely
-
Above applies to "n"/"nn", and "s"/"ss" as well
-
Format "AMPM" uses settings from WIN.INI [intl] s1159=AM, s2359=PM
Try mixing and matching the format strings |
Expression |
Result |
Format(36715.5784, "m-d-yy") |
7-8-00 |
Format(36715.5784, "d-mmmm-y") |
8-July-00 |
Format(36715.5784, "mmmm yyyy") |
July 2000 |
Format(36715.5784, "hh:mm a/p") |
01:52 p |
Format(36715.5784, "m/d/yy h:mm") |
7/8/00 13:52 |
New VB 6.0 formatting functions
FormatCurrency, FormatPercent, FormatNumber
Previously, you would have used code similar to:
result = Format(324.45, "currency")
result = Format(324.45, "percent")
result = Format(324.45, "standard")
These methods would now be considered deprecated in favor of:
result = FormatCurrency(324.45)
result = FormatPercent(324.45, 0)
result = FormatNumber(324.45, 2)
Bill's new formatting functions includes 4 optional arguments for specifying the number of digits after the decimal, and whether or not to use a leading 0, parentheses for negative values, or a comma for thousands separator. If you would just as soon use the settings from the user's Control Panel, ignore the last 4 arguments.
'Use default Control Panel Regional Settings
result = FormatCurrency(number)
'Default digits after decimal, no leading zeros or thousands separator
result = FormatNumber(number, , False, , False)
'One digit after decimal, no parentheses
result = FormatPercent(number, 1, , False)
FormatDateTime
This new function works about the same as the regular Format function, but you're only allowed to use one of 5 constants - vbGeneralDate, vbLongDate, vbShortDate, vbLongTime, vbShortTime. I don't see this function as any kind of language improvement.
|