FormatDateTime - VBScript


Overview

The FormatDateTime command in VB Script allows you to format a date or time value into a custom string representation. It provides a flexible way to display date and time information in the desired format for various purposes.

Syntax

FormatDateTime(date, format)

Parameters:

  • date: The date or time value to format, in date or time literal or a valid date/time variable.
  • format: A string specifying the format in which to display the date or time. See below for supported format options.

Options/Flags

The following format characters can be used in the format string to customize the date/time output:

| Format Character | Description |
|—|—|
| d | Day of the month as a number without leading zero (1-31) |
| dd | Day of the month as a number with leading zero (01-31) |
| ddd | Abbreviated weekday name (Sun-Sat) |
| dddd | Full weekday name (Sunday-Saturday) |
| M | Month as a number without leading zero (1-12) |
| MM | Month as a number with leading zero (01-12) |
| MMM | Abbreviated month name (Jan-Dec) |
| MMMM | Full month name (January-December) |
| y | Year as a two-digit number (00-99) |
| yy | Year as a four-digit number (0000-9999) |
| h | Hour (0-23) |
| hh | Hour with leading zero (00-23) |
| m | Minute (0-59) |
| mm | Minute with leading zero (00-59) |
| s | Second (0-59) |
| ss | Second with leading zero (00-59) |

Additionally, you can separate date and time components with spaces, colons, or other characters as needed in the format string.

Examples

Simple Formatting:

WScript.Echo FormatDateTime(Now, "dd/MM/yy") ' Output: 15/08/23

Custom Formatting:

WScript.Echo FormatDateTime(Now, "MMMM d, yyyy hh:mm:ss tt") ' Output: August 15, 2023 03:22:45 PM

Formatting with User-Defined Separators:

WScript.Echo FormatDateTime(Now, "dd-MMM-yyyy hh:mm") ' Output: 15-Aug-2023 03:22

Common Issues

  • Ensure that the date or time value provided is valid.
  • Double-check the format string to match desired output.
  • Use leading zeros where necessary to maintain the accuracy of the formatted representation.

Integration

The FormatDateTime command can be integrated with other VB Script commands to create custom scripts for reporting, data manipulation, and various automation tasks.

  • now
  • isdate
  • datediff