Home > Batch File, Programming > Batch File ISO 8601 Date Format – Version 2.0

Batch File ISO 8601 Date Format – Version 2.0

Last year I wrote about using the ISO 8601 date format in Batch Files. One of the issues I had was that times before 10:00 were not preceded with a zero.

The solution was quite simple, in that you can use an in-built replace function. For example, the following creates a variable called MyTime that has any spaces (ASCII 32) replaced with a zero (0).

SET MyTime=%time: =0%

So I’ve used my previous code to generate the ISO date into a variable and then replaced any spaces within that variable with a zero. I’ve also update the code to use the correct delimiters for ISO 8601 and given a few examples that you can copy and paste into your own code.

@ECHO OFF
CLS
ECHO ISO 8601 date and time formats in Batch files.
ECHO The following are generated from the system date (%date%) and time (%time%).
ECHO.


REM ISO 8601 Date and Time in extended format (YYYY-MM-DDTHH:MM:SS)
SET isodt=%date:~6,4%-%date:~3,2%-%date:~0,2%T%time:~0,2%:%time:~3,2%:%time:~6,2%
SET isodt=%isodt: =0%
ECHO ISO 8601 Date and Time in extended format (YYYY-MM-DDTHH:MM:SS): %isodt%

REM ISO 8601 Date and Time in basic format (YYYYMMDDTHHMMSS)
SET isodt=%date:~6,4%%date:~3,2%%date:~0,2%T%time:~0,2%%time:~3,2%%time:~6,2%
SET isodt=%isodt: =0%
ECHO ISO 8601 Date and Time in basic format (YYYYMMDDTHHMMSS): %isodt%


REM ISO 8601 Date in extended format (YYYY-MM-DD)
SET isodt=%date:~6,4%-%date:~3,2%-%date:~0,2%
SET isodt=%isodt: =0%
ECHO ISO 8601 Date in extended format (YYYY-MM-DD): %isodt%

REM ISO 8601 Date in basic format (YYYYMMDD)
SET isodt=%date:~6,4%%date:~3,2%%date:~0,2%
SET isodt=%isodt: =0%
ECHO ISO 8601 Date in basic format (YYYYMMDD): %isodt%


REM ISO 8601 Time in extended format (HH:MM:SS)
SET isodt=%time:~0,2%:%time:~3,2%:%time:~6,2%
SET isodt=%isodt: =0%
ECHO ISO 8601 Time in extended format (HH:MM:SS): %isodt%

REM ISO 8601 Time in basic format (HHMMSS)
SET isodt=%time:~0,2%%time:~3,2%%time:~6,2%
SET isodt=%isodt: =0%
ECHO ISO 8601 Time in basic format (HHMMSS): %isodt%


REM ISO 8601 Date and Time (not including seconds) in basic format (YYYYMMDDTHHMM)
SET isodt=%date:~6,4%%date:~3,2%%date:~0,2%T%time:~0,2%%time:~3,2%
SET isodt=%isodt: =0%
ECHO ISO 8601 Date and Time (not including seconds) in basic format (YYYYMMDDTHHMM): %isodt%
Categories: Batch File, Programming Tags: , ,
  1. 15th August, 2013 at 20:47

    Thanks you, i use SET isodt=%date:~6,4%-%date:~3,2%-%date:~0,2%

    It’s possible make a set of “yesterday”

    Thanks !!!

  2. 16th August, 2013 at 18:39

    Hi evvivame,

    Glad you found it useful. It’s a bit more difficult to get yesterday’s date in a batch file as we have to do some complicated maths. I did find this blog, however, which may be of use: http://www.powercram.com/2010/07/get-yesterdays-date-in-ms-dos-batch.html

    Hope it helps.

  3. Prashanth
    20th August, 2014 at 08:49

    Fantastic script, thank you.

  4. 2nd April, 2015 at 18:02

    Warning. Probably locale sensitive. And/or may not work on older windows. Example, windows2000 box with US locale, output is incorrect:

    ISO 8601 date and time formats in Batch files.
    The following are generated from the system date (Thu 04/02/2015) and time (12:$

    ISO 8601 Date and Time in extended format (YYYY-MM-DDTHH:MM:SS): /02/-00-ThT12:$
    ISO 8601 Date and Time in basic format (YYYYMMDDTHHMMSS): /02/00ThT125807
    ISO 8601 Date in extended format (YYYY-MM-DD): /02/-00-Th
    ISO 8601 Date in basic format (YYYYMMDD): /02/00Th
    ISO 8601 Time in extended format (HH:MM:SS): 12:58:07
    ISO 8601 Time in basic format (HHMMSS): 125807
    ISO 8601 Date and Time (not including seconds) in basic format (YYYYMMDDTHHMM):$

  5. 2nd April, 2015 at 18:05

    Oops… that incorrect output (with “Th” from Thursday) from windows2000 box was actually as shown below. Probably need some region-independent code such as at:
    http://stackoverflow.com/questions/203090/how-to-get-current-datetime-on-windows-command-line-in-a-suitable-format-for-us

    ISO 8601 date and time formats in Batch files.
    The following are generated from the system date (Thu 04/02/2015) and time (12:58:07.75).

    ISO 8601 Date and Time in extended format (YYYY-MM-DDTHH:MM:SS): /02/-00-ThT12:58:07
    ISO 8601 Date and Time in basic format (YYYYMMDDTHHMMSS): /02/00ThT125807
    ISO 8601 Date in extended format (YYYY-MM-DD): /02/-00-Th
    ISO 8601 Date in basic format (YYYYMMDD): /02/00Th
    ISO 8601 Time in extended format (HH:MM:SS): 12:58:07
    ISO 8601 Time in basic format (HHMMSS): 125807
    ISO 8601 Date and Time (not including seconds) in basic format (YYYYMMDDTHHMM): /02/00ThT1258

    • 2nd April, 2015 at 19:27

      Hi vanilla,

      Yes, you’re absolutely correct the command prompt “DATE” variable will return a string based on the locale of the system. For example, in the UK when I type ECHO %date% at a command prompt, it responds with: 02/04/2015.

      The script uses absolute positions to get the year, month and day (these are the bits in the script like %date:~6,4%). With a bit of experimentation you should be able to obtain the information you require (It was mentioned on my original post that other locales may not work: https://3rdlinesupport.wordpress.com/2012/11/18/batch-file-iso-8601-date-format/).

      There is one issue, in they if the day of the week is returned before the date, the script may not be able to use absolute locations to get the day, month or year. i.e. “Wednesday 04/01/2015” has the date starting at position 11, compared to “Thursday 04/02/2015” which has the date starting at position 10). It looks like your output has three letters for each day of the week, so you may be okay with that.

      For universal compatibility we could write a quick vbScript to either return the date in ISO format, or put it into an environment variable. I think Windows 2000 supports vbScript – if I recall correctly it was included with IE5.

      Let me know if you need any help with this.

      Regards,

      Jonathan

  1. 16th March, 2013 at 15:25

Leave a comment