Now - VBScript


Overview

The Now function in VB Script returns the current system date and time as a Date object. It is primarily used to obtain the current date and time for various purposes, such as logging, data analysis, and time-sensitive tasks.

Syntax

Now()

Options/Flags

This function has no options or flags.

Examples

Example 1: Getting the Current Date and Time

' Display the current date and time
MsgBox Now

Example 2: Logging the Time of an Event

' Create a log file and write the current time
Dim fs
Set fs = CreateObject("Scripting.FileSystemObject")
fs.OpenTextFile("log.txt", 8, True).WriteLine "Event occurred at " & Now

Example 3: Calculating Time Elapsed

' Record the start time
Dim startTime = Now

' Perform some operations

' Calculate the elapsed time
Dim elapsedTime = Now - startTime
MsgBox "Elapsed time: " & elapsedTime

Common Issues

  • Incorrect time zone: The Now function returns the system date and time, which can be affected by the user’s time zone settings. If the expected date and time do not match, verify the time zone settings.
  • Type mismatch: The Now function returns a Date object, so attempting to assign it to a variable of a different type, such as a string, may result in a type mismatch error.

Integration

The Now function can be combined with other VB Script commands for advanced tasks:

  • **With DateAdd: To add or subtract time intervals from the current date and time.
  • **With DateFormat: To format the output date and time according to a specified format string.
  • Date: Returns a Date object representing a specified date.
  • Time: Returns a Date object representing a specified time.
  • Timer: Returns the number of milliseconds since midnight.