Chris F.A. Johnson
Unix shell programming notes

16 September 2013

Date functions

This collection of functions for testing and manipulating dates makes use of bashisms and many, if not most, will not work in other shells. Some may work in the Korn shell (esp. KornShell93).

INDEX
date_vars
Populate variables with today's date
USAGE: date_vars [VAR ...]
Global variables set: today year month day jday bdate wknum
datenum
Convert DATE to day NUMBER in C.E.
USAGE: datenum [DATE [VAR]]
day_of_week
Convert ISO date (YYYY-MM-DD) to day of the week (Sunday = 0)
USAGE: day_of_week DATE [shortname|dayname|VAR]
days_in_month
Return number of days in given month
USAGE: days_in_month [VAR] MONTHNUM [year]
Required functions: is_leap_year(), is_var()
Global variables set: days_in_month
daysdiff
Find the number of days between two dates
USAGE: daysdiff DATE1 DATE2 [VAR]
err
Return from function with error message
USAGE: command || err ERRNUM [MESSAGE] || return
is_date
Return success if argument is a valid date
USAGE: is_date STRING ...
Required functions: days_in_month(), is_leap_year()
is_leap_year
Return successfully if YEAR (or current year) is a leap year
USAGE: is_leap_year [YEAR]
is_var
Assign STRING to VAR if it's a valid variable name
USAGE: is_var STRING
jday
Return day of year for given day
USAGE: jday DATE [VAR]
Required functions: is_date() is_leap_year() is_var()
numdate
Convert day number to calendar date
USAGE: numdate DAYNUM [VAR]
parsedate
Parse various formats of date
USAGE: parsedate ...
qdate
Print information about a given date number
USAGE: qdate YYYYMMDD
qnum
Print information about any number
USAGE: qnum NUMBER
qnum2html
Convert output of qnum() to HTML
USAGE: qnum2html NUM
random_date
Return random date in given range of years
USAGE: random_date FIRST LAST [VAR]
Global variables set: random_date [VAR]
split_date
Splite date[time] string into year month day [hour minute second]
USAGE: split_date STRING ...
Global variable set: split_date[year month day hour minute second]
today
Return today's date
USAGE: today [VAR]

Modified 18 Nov 2021