Archive

Archive for March, 2013

Batch File ISO 8601 Date Format – Version 2.0

16th March, 2013 7 comments

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: , ,