Bart's Smarts

Smart .NET, Silverlight, and SharePoint Development.

Posts Tagged ‘Utilities’

Calculating relative (offset) dates for DOS commands and scheduled tasks

Posted by sapientcoder on March 5, 2009

I’ve had several cases come up over the years where I’ve needed to pass a date to a program that’s being run from the Windows command line (DOS prompt) or as a Windows Scheduled Task. Since different programs require dates for different meanings and in different formats, I’ve always thought it would be nice if there were a “standardized” way to calculate whatever date needs to be passed to the program and then format it in a way the program expects.

Up until now, the most common way I’ve seen documented on the web to do this is to parse the built-in %Date% variable (or the result of the ‘date’ command) into multiple variables that can then be strung together to generate a date in the desired format.

In my opinion, this approach has a couple of shortcomings:

  1. When a variable is set using the SET command in a DOS session, the variable retains its value until the DOS session is terminated. In other words, you must close and re-open the command prompt before its value can be reset. This is inconvenient if you want to re-run a program several times in the same session and pass a different date each time.
  2. Parsing the date returned by the ‘date’ command or %Date% variable is machine-specific and can break if the date settings on the machine are changed.

To avoid these issues, I used a different approach when I recently had the need to do this.

I wrote a small batch file that does two things. First, it calls a small utility program I wrote called getrelativedate to get a date relative to the current date and format it. Second, it calls the application that needs the date and passes it the output from getrelativedate.

Here’s a sample batch file that uses this approach to pass a date 5 days in the past to an application:

@echo off

FOR /F %%a IN ('path\to\getrelativedate -5 yyyyMMdd') DO (
    if errorlevel 0 (
        path\to\prog\expecting\date -date %%a -other params
    )
)

And here’s the syntax for the getrelativedate utility:

getrelativedate <interval> <format>

The first parameter, interval, designates the date offset and is formatted in a way that’s accepted by .NET’s TimeSpan.Parse() method. See that method’s documentation to see how interval should be formatted. It’s pretty easy. Also, intervals can be positive (in the future), negative (in the past), or 0 (if you just want to format the current date and don’t need an offset).

The second parameter, format, is a standard .NET date format string (like what you would pass to the DateTime.ToString() method).

Pretty simple and easy but also quite flexible and powerful, in my opinion.

If you’re interested, download getrelativedate.zip.

The zip file includes the utility program, a sample batch file, and a readme file with documentation.

Posted in .NET | Tagged: | Leave a Comment »