Date and Time Functions
Overview
Note: Any attempt to optimise the arithmetic used in these functions will probably cause the functions to fail under NT4 - as contrary to Microsoft's documentation, parantheses are not supported and arithmetic operator precedence is incorrect.
Parameters
When passing dates and times to the Date and Times Functions, the following formats must be observed:-
Years must be two or four digits. Years in the range 70 to 99 will be treated as years from the twentieth century, years from 00 to 69 as the twenty-first.
Months must be one or two digits in the range 1 to 12 or 01 to 12.
Day of month must be one or two digits in the range 1 to 31 or 01 to 31.
Hours must be in 12 or 24 hour format. For 12 hour format the hours must be in the range 1 to 12 or 01 to 12. If 12 hour format is used, then the minutes parameter must be immediately followed by 'a' or 'p' (eg 00a or 34p). For 24 hour format, the hours must be in the range 0 to 23 or 00 to 23.
Minutes must be two digits in the range 00 to 59.
Seconds must be two digits in the range 00 to 59.
The reason for accepting two digit years and 12 hour times is mainly for compatibilty with the output of the DIR command, which may use one or both of these formats.
Return Values
The Date and Time Functions that return dates and/or times will use the following formats:-
Years will be four digits.
Months will be two digits in the range 01 to 12.
Day of month will be two digits in the range 01 to 31.
Hours will be 24 hour format in the range 00 to 23.
Minutes will be two digits in the range 00 to 59.
Seconds will be two digits in the range 00 to 59.
These formats will be suitable for most applications and make it simple to use ISO 8601 formatted dates. Any leading zeros can be easily removed if required by prefixing the value with 100 and then taking modulus 100. For example:-
@echo off & setlocal ENABLEEXTENSIONS
set var=09
set /a var=100%var%%%100
echo/Leading zero has been removed: %var%
Suggested Formats
This is basically a very brief summary of the most commonly used date and time formats suggested by ISO 8601.
Component Basic Format Extended Format
Calendar Date YYYYMMDD Eg 19991231 YYYY-MM-DD Eg 1999-12-31
Time of day hhmmss Eg 235909 hh:mm:ss Eg 23:59:09
Date and Time YYYYMMDDThhmmss
Eg 19991231T235909 YYYY-MM-DDThh:mm:ss
Eg 1999-12-31T23:59:09
Ordinal Date YYYYDDD Eg 1999365 YYYY-DDD Eg 1999-365
Week Date YYYYWwwD Eg 1999W525 YYYY-Www-D Eg 1999-W52-5 |
|