How to Create Timestamps in Batch Files

Written by alexander gokhfeld
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Create Timestamps in Batch Files
DOS batch files include timestamps by using date and time commands. (Jupiterimages/ Images)

You create and use batch files to automate different system administration tasks, such as backups, program and database updates or recording errors, when an application problems occur. DOS commands that are used in the batch files can include a variable, such as date and time. By using the string of the output from the date and time DOS commands you can create timestamps in the batch files to record time information during execution of system maintenance procedures.

Skill level:

Other People Are Reading


  1. 1

    Click "Start," "Notepad" to open Notepad editing program.

  2. 2

    Click "File," "New."

  3. 3

    Enter the following commands in the open window:

    @echo off
    rem Timestamp Generator
    rem Parse the date (e.g., Fri 02/08/2008)
    set cur_yyyy=%date:~10,4%
    set cur_mm=%date:~4,2%
    set cur_dd=%date:~7,2%
    rem 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%
    rem Set the timestamp format
    set timestamp=%cur_yyyy%%cur_mm%%cur_dd%-%cur_hh%%cur_nn%%


    rem Do something with it
    echo Timestamp: %timestamp%
    rem Clear the environment variables
    set cur_yyyy=
    set cur_mm=
    set cur_dd=
    set cur_hh=
    set cur_nn=
    set cur_ss=
    set cur_ms=
    set timestamp=
  4. 4

    Click "File," "Save." Enter "mybatchfile.bat" in the "Save As" box. Click "OK" to create the batch file with timestamps.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.