Sample Macro

Date String to DateTime Variable

Written by Kevin Heaton, Insight Software Solutions

What it does

This sample macro shows how to change a date string to a DateTime variable using the ‘Run Macro in Variable’ command.

To install

Download the macro file umdatestringtodatetimevar.mex and save it to your hard drive. Open the macro file or import the macro into your existing macro file by clicking File, Import, Import Macros.

To use

When you run the macro ‘Sample: MM-DD-YY to DateTime var’ the result will be displayed in a Text Box. However, this macro will provide the most benefit if you study how it works and modify it for your own use.

How it works

This macro first parses the date into individual parts: Month, Day and Year. It then creates a ‘Date/Time’ command string and then uses the ‘Run Macro In Variable’ to perform the conversion of the date string into a DateTime variable.

Every macro command can be viewed two different ways: as it appears in the Script Editor and in ASCII format as it appears in the Direct Editor. In the Direct Editor the ‘Date/Time’ command looks like this:

<DATE/TIME Format=”DD-MMM-YYYY” Flags=”\x81″ Date=”11/12/2012″ Day_Offset=”0″ Month_Offset=”0″ Year_Offset=”0″ Hour_Offset=”0″ Second_Offset=”0″ Left=”Center” Top=”Center” Monitor=”0″ Variable=”%dtResult%” IsDateVar=”TRUE” _IGNORE=”0x0002″/>

This macro uses the ‘Variable Set String’ command to put this information into a string that can be used by the ‘Run Macro in Variable’ command. There is one thing to take care of, however.

When variables such as %Month%, %Day% or %Year% are used in the ‘Variable Set String’ command they are converted to the content of the variable. In our example, %Month% is converted to 11, %Day% is converted to 12 and %Year% is converted to 2012. This is what we want for these values. But if we use %dtResult% for the result variable it will be converted to the content of %dtResult% which, in this case, will cause the macro command to fail. To avoid the automatic conversion of a variable use \c25 in place of the % characters. So the result variable becomes \c25dtResult\c25, like this:

<DATE/TIME Format=”DD-MMM-YYYY” Flags=”\x81″ Date=”%Month%/%Day%/%Year%” Day_Offset=”0″ Month_Offset=”0″ Year_Offset=”0″ Hour_Offset=”0″ Second_Offset=”0″ Left=”Center” Top=”Center” Monitor=”0″ Variable=”\c25dtResult\c25″ IsDateVar=”TRUE” _IGNORE=”0x0002″/>

Once the command in string format is in a variable the ‘Run Macro in Variable’ command puts the date into the variable %dtResult%.

Limitations

It may be possible to crash Macro Express Pro if you provide an invalid date string. You should add commands to make sure the input variable contains a valid date.

Requirements

Macro Express Pro