How to Generate a Formatted Date/Time Using Expressions

Applies To: AutoMate 6, AutoMate 5
Published: 11/30/04 , modified May 16, 2006


Occasionally you may require AutoMate to generate formatted text representing the current date/time, or the date/time offset by a certain number of days/months/or years. Usually this text value is used to rename a file or create a folder for file archiving purposes. Alternatively, it may be used for data entry when sending keystrokes to an application when a date or date range is needed and will vary depending on what day the task is run. This can be accomplished in one of two ways: either by using expressions or via the Format Date/Time action.

Using an expression is the most compact method of returning a formatted date/time as it can be placed directly into the step where the text should be used. However, using an expression is generally considered more difficult than using the Format Date/Time action.

To begin, you should familiarize yourself with expressions in AutoMate, understanding that an expression can by used in any task step and if surrounded in percent signs it will be replaced with it's result at run time.

Returning the Current Date Time:

The functions we will be using are:

  • Now() - returns the current date and time
  • Format() - formats a date / time to the desired "mask"

<AMMESSAGEBOX>The current date time is: %Now()%</AMMESSAGEBOX>

By running the above step, we see a Message Box displayed containing the text:

  • The current date time is: 11/7/2003 10:05:52 AM

Formatting the Current Date Time:

In most cases we will want to format this date time to match our needs. For example, if we wanted to use this value in a filename we would need to eliminate the forward slashes as these characters are not allows in filenames (according to Windows). In some other cases we may want to format the date to a format expected by the application to which we are trying to Send Keystrokes to.

The Format function requires a date as a parameter and a "mask" which represents the format in which you want the date returned in. In this case the date we want formatted is today, so we use Now(). The mask we want is yyyyddmm (four digit year, two digit day, two digit month - no spaces or separators). Our step looks like this:

<AMMESSAGEBOX>The current date time is: %Format(Now(),'yyyymmdd')%</AMMESSAGEBOX>

When we run this step a message box is displayed with the following text:

  • The current date time is: 20031107

This value can be used in any step; for instance in the File Write action, if you wanted to write a new file each day with a unique filename based on the current date - you could use the following step:

<AMFILEWRITE FILE="c:\foldername\%Format(Now(),'yyyymmdd')%.txt" APPEND="NO">Hello World</AMFILEWRITE>

If the task was run once every day, this would create a new filename based on the date and time each day it was run. Remember, the expression can be used in any parameter in any AutoMate action. At runtime, when AutoMate sees the percent-signs, it knows that it needs to resolve the expression within. In this case, the expression is: Format(Now(),'yyyymmdd')

Returning Yesterday's Date Time:

Occasionally you will want to use a date/time value that is not today's date, but yesterday's date, or the date one month ago, or some other date offset from the current date. This is accomplished by using the DateAdd() function. Please see the documentation for DateAdd for a complete list of possible parameter values. This function takes 3 parameters, first, the type of unit by which we are going to add or subtract (years, months, days, hours, minutes, seconds); second, the actual number of whatever unit we are using to add or subtract (positive number adds, negative number subtracts); and last, what date to start from. In this case that date will be Now(), because we want to get yesterday's date.

<AMMESSAGEBOX>Yesterday's date time was: %DateAdd('d',-1, Now())%</AMMESSAGEBOX>

When the above step is run, the text returned is:

  • Yesterday's date time was: 11/6/2003 10:21:19 AM

Today's date is November 7th 2003, so yesterdays date is November 6.

Formatting Yesterday's Date Time:

In most cases, just as with the first example using the current date, you will want to format this date value.

To do this simply combine what we have already learned in the second example with the third. Take the expression used to format the current date:

<AMMESSAGEBOX>The current date time is: %Format(Now(),'yyyymmdd')%</AMMESSAGEBOX>

and replace Now() with the expression we used to get yesterdays date:

<AMMESSAGEBOX>The current date time is: %Format(DateAdd('d',-1, Now()),'yyyymmdd')%</AMMESSAGEBOX>

So instead of formatting Now(), we are formatting the result of the DateAdd function which is returning yesterday because a negative number was used. Upon running this step, the following text is returned:

  • The current date time is: 20031106


As demonstrated here, AutoMate is capable of generating most any date you might require, and the date can be returned in any format that is needed by simply changing a few of the parameters of the functions we reviewed. It is recommended that you review the AutoMate help file on the following topics to further enhance your knowledge of these functions and how they work:

  • Using Expressions
  • Now() function
  • Format() function
  • DateAdd() function