Using time and dates in your windows batch scripts


Using date and time in your batch scripts can be quite difficult. The current date and time can be easy enough but if you want anything more complex like yesterday’s date, you’re out of luck.

Here’s a simple VBscript that can create a batch file to use with your script that will set several environment variables that you can then use.

For example:

REM My Test script 
cscript /nologo envtime.vbs > envtime.bat 
call envtime.bat 
echo Yesterday's date was %YDOM%/%YMON%/%YYEAR%

which prints out the date yesterday taking into account months and years etc.

It also supports the date 2 and 3 days ago using %TWODOM% and %THREEDOM% respectively. See the script for more syntax.

If later in your script you want an updated time and date, e.g. at the end of the script, just rerun the lines to generate and call the batch file using the lines in the example above.

'#############################################################################################
'# VERSION HISTORY                                                                           #
'#                                                                                           #
'# Date          Version    Author          Details                                          #
'#                                                                                           #
'# 2007/04/19    1.0        Stuart Jordan   Creation                                         #
'# 2007/04/24    1.1       Stuart Jordan  Changed date/time funcs to use day/Month/Year   #
'# 2010/09/20    2.0       Stuart Jordan  Made the month/day/year functions be able to    #
'#                                          return any number of days before/after today.    #
'#                                                                                           #
'#############################################################################################

' ######################################
' # Main body
' #
wscript.echo "@echo off"
wscript.echo
wscript.echo "REM Four digit year"
wscript.echo "set YEAR=" & get_year(0)
wscript.echo
wscript.echo "REM Four digit previous year"
wscript.echo "set PYEAR=" & get_pyear()
wscript.echo
wscript.echo "REM Two digit year"
wscript.echo "set YR=" & get_yr(0)
wscript.echo
wscript.echo "REM Two digit previous year"
wscript.echo "set PYR=" & get_pyr()
wscript.echo
wscript.echo "REM Two digit month"
wscript.echo "set MON=" & get_mon(0)
wscript.echo
wscript.echo "REM Two digit day of the month"
wscript.echo "set DOM=" & get_dom(0)
wscript.echo
wscript.echo "REM Two digit hour"
wscript.echo "set HOUR=" & get_hour()
wscript.echo
wscript.echo "REM Two digi minute"
wscript.echo "set MIN=" & get_min()
wscript.echo
wscript.echo "REM Yesterday's day of the month"
wscript.echo "set YDOM=" & get_dom(-1)
wscript.echo
wscript.echo "REM Yesterday's two digit month"
wscript.echo "set YMON=" & get_mon(-1)
wscript.echo
wscript.echo "REM Yesterday's four digit year"
wscript.echo "set yyear=" & get_year(-1)
wscript.echo
wscript.echo "REM Yesterday's two digit year"
wscript.echo "set yyr=" & get_yr(-1)
wscript.echo
wscript.echo "REM Text month"
wscript.echo "set TXTMON=" & get_txtmon(0)
wscript.echo
wscript.echo "REM Yesterday's Text month"
wscript.echo "set YTXTMON=" & get_txtmon(-1)
wscript.echo
wscript.echo "REM 2 days ago two digit day of the month"
wscript.echo "set TWODOM=" & get_dom(-2)
wscript.echo
wscript.echo "REM 2 days ago two digit month"
wscript.echo "set TWOMON=" & get_mon(-2)
wscript.echo
wscript.echo "REM 2 days ago four digit year"
wscript.echo "set TWOYR=" & get_year(-2)
wscript.echo
wscript.echo "REM 2 days ago two digit year"
wscript.echo "set TWOYR=" & get_yr(-2)
wscript.echo
wscript.echo "REM 3 days ago two digit day of the month"
wscript.echo "set THREEDOM=" & get_dom(-3)
wscript.echo
wscript.echo "REM 3 days ago two digit month"
wscript.echo "set THREEMON=" & get_mon(-3)
wscript.echo
wscript.echo "REM 3 days ago four digit year"
wscript.echo "set THREEYEAR=" & get_year(-3)
wscript.echo
wscript.echo "REM 3 days ago two digit year"
wscript.echo "set THREEYR=" & get_yr(-3)
wscript.echo
wscript.echo "REM Digit Day of the week"
wscript.echo "set DOW=" & get_dow()
wscript.echo
wscript.echo "REM Text Day of the week"
wscript.echo "set TXTDOW=" & get_txtdow()
wscript.echo
wscript.echo "REM Yesterday's Text Day of the week"
wscript.echo "set TXTDOW=" & get_ytxtdow()
wscript.echo
wscript.echo "REM done"

' ######################################
' # Functions
' #
' Four digit year

private function get_ydateserial(intSelection,numDays)
    strtmpDate=dateserial(datepart("yyyy",date()),datepart("m",date()),datepart("d",date())+numDays)
    'wscript.echo "dateserial=" & strtmpDate
    arrtmpDate=split(strtmpDate, "/")
    get_ydateserial=arrtmpDate(intSelection)
end function

' Two digit day of the month
function get_dom(numDays)
    get_dom=fixuptwodigit(get_ydateserial(0,numDays))
end function

' Two digit month
function get_mon(numDays)
    'get_mon=fixuptwodigit(Month(date()))
    get_mon=fixuptwodigit(get_ydateserial(1,numDays))
end function

function get_year(numDays)
    get_year=get_ydateserial(2,numDays)
end function

' Two digit year
function get_yr(numDays)
    get_yr=right(get_year(0), len(get_year(0)) - 2)
end function

' Four digit previous year
function get_pyear()
    get_pyear=get_year(0) -1
end function

' Two digit prevous year
function get_pyr()
    get_pyr=right(get_year(0)-1, len(get_year(0)) - 2)
end function

' Two digit hour
function get_hour()
    get_hour=fixuptwodigit(Hour(now()))
end function

' Two digit minute
function get_min()
    get_min=fixuptwodigit(Minute(now()))
end function

' Today's month in text
function get_txtmon(numDaus)
    get_txtmon=get_montotxt(get_mon(numDays))
end function

' Convert the two digit month to text
function get_montotxt(intMon)
    select case intMon
        case 1
            get_montotxt="Jan"
        case 2
            get_montotxt="Feb"
        case 3
            get_montotxt="Mar"
        case 4
            get_montotxt="Apr"
        case 5
            get_montotxt="May"
        case 6
            get_montotxt="Jun"
        case 7
            get_montotxt="Jul"
        case 8
            get_montotxt="Aug"
        case 9
            get_montotxt="Sep"
        case 10
            get_montotxt="Oct"
        case 11
            get_montotxt="Nov"
        case 12
            get_montotxt="Dec"
    end select
end function

function get_dow()
    get_dow=WeekDay(date)
end function

function get_txtdow()
    get_txtdow=WeekDayName(get_dow())
end function

function get_ytxtdow()
    get_ytxtdow=WeekDayName(get_dow()-1)
end function

' Change 1 digit numbers to two (for months and days)
function fixuptwodigit(strtmp)
    tmpEntry=""
    if len(strtmp)<2 then
        tmpEntry="0"
    end if
    fixuptwodigit=tmpEntry & strtmp
end function

Click the link below to download the script

Download

Advertisements
Tagged ,

2 thoughts on “Using time and dates in your windows batch scripts

  1. Kalle says:

    1) You make it dependent on regional settings by doing split on / instead of using datepart to create the array (!)
    2) Don’t call date() multiple time as it could create issues

  2. buy d3 gold says:

    This is definitely the thing I’ve been looking for! Superb and thanks very much!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: