DOS/Windows Batch GET_TIMESTAMP Function

Most of the programming professionals need to get date & time in required format using Windows & DOS batch script.
One of the major use is to create log file and folders using date and timestamp. Here is a BATCH function to get the time stamp in your desire format (“MM-DD-YYYY” etc.)

You can use any combination of characters mentioned below to get your appropriate format.

Character Purpose Example
YYYY To get 4 digit year 2013
YY To get 2 digit year 13
MMMM To get full month name January
MMM To get short month name Jan
MM To get 2 digit month number 01
DDDD To get full day name Monday
DDD To get short day name Mon
DD To get 2 digits date 31
HH To get 2 digits hour 13
MI To get 2 digits minute 01
SS To get 2 digits second 01
MS To get 2 digit millisecond 44
B To add space in format Space
C To add comma in format Comma “,”
Special Characters To add desire character in format(space and comma is not allowed here) _ – : / etc.

Usage:

Image

Add this function in your batch file and call it as below:

SET vMyTimeStamp=""
CALL :GET_TIMESTAME DD-MMM-YYYY vMyTimeStamp
:: to check output, you can echo the result of this function.
:: ECHO %vMyTimeStamp%

Different Examples of formats:

MMM-DD-YYYYBHH:MI:SS.MS
MM/DD/YYBHH:MI:SS
DDDDCBDD-MMMCYYYY
YYYYMMDD_HHMISSMS

Function Code:

:GET_TIMESTAMP %myFormat% vResult
     SET RepFormat=%1
     SET MShortMap=01-Jan;02-Feb;03-Mar;04-Apr;05-May;06-Jun;07-Jul;08-Aug;09-Sep;10-Oct;11-Nov;12-Dec
     SET MLongMap=01-January;02-February;03-March;04-April;05-May;06-June;07-July;08-August;09-September;10-October;11-November;12-December
     SET DShortMap=01-Mon;02-Tue;03-Wed;04-Thu;05-Fri;06-Sat;07-Sun
     SET DLongMap=01-Monday;02-Tuesday;03-Wednessday;04-Thursday;05-Friday;06-Saturday;07-Sunday
     SET DShort2LongMap=Mon-Monday;Tue-Tuesday;Wed-Wednessday;Thu-Thursday;Fri-Friday;Sat-Saturday;Sun-Sunday
     :: a) Parse the date (e.g., Fri 02/08/2008)
           set cur_yyyy=%date:~10,4%
           set cur_yy=%date:~12,2%
           set cur_mm=%date:~4,2%
           set cur_dd=%date:~7,2%
           set cur_ddd=%date:~0,3%
     :: b) Parse the time (e.g., 11:17:13.49)
           set cur_hh=%time:~0,2%
           if %cur_hh% lss 10 (set cur_hh=0%time:~1,1%)
           set cur_nn=%time:~3,2%
           set cur_ss=%time:~6,2%
           set cur_ms=%time:~9,2%
     ::
     CALL SET cur_mmm=%%MShortMap:*%cur_mm%-=%%
          SET cur_mmm=%cur_mmm:;=&rem.%
     ::
     CALL SET cur_mmmm=%%MLongMap:*%cur_mm%-=%%
          SET cur_mmmm=%cur_mmmm:;=&rem.%
     ::
     CALL SET cur_dddd=%%DShort2LongMap:*%cur_ddd%-=%%
          SET cur_dddd=%cur_dddd:;=&rem.%
     ::
     CALL SET RepFormat=%%RepFormat:B= %%
     CALL SET RepFormat=%%RepFormat:C=,%%
     CALL SET RepFormat=%%RepFormat:YYYY=%cur_yyyy%%%
     CALL SET RepFormat=%%RepFormat:MMMM=%cur_mmmm%%%
     CALL SET RepFormat=%%RepFormat:MMM=%cur_mmm%%%
     CALL SET RepFormat=%%RepFormat:DDDD=%cur_dddd%%%
     CALL SET RepFormat=%%RepFormat:DDD=%cur_ddd%%%
     CALL SET RepFormat=%%RepFormat:YY=%cur_yy%%%
     CALL SET RepFormat=%%RepFormat:MM=%cur_mm%%%
     CALL SET RepFormat=%%RepFormat:DD=%cur_dd%%%
     CALL SET RepFormat=%%RepFormat:HH=%cur_hh%%%
     CALL SET RepFormat=%%RepFormat:MI=%cur_nn%%%
     CALL SET RepFormat=%%RepFormat:SS=%cur_ss%%%
     CALL SET RepFormat=%%RepFormat:MS=%cur_ms%%%
     SET "%~2=%RepFormat%"
GoTo :EOF

Leave a comment