Low Orbit Flux Logo 2 D

AWK How To Print Date and Time

Printing the date in AWK is easy if you are actually using gawk ( you probably are if you are using Linux ).

The easy way to print the time and date with AWK is to just call the strftime function with no parameters like this. It will give you the current date and time with a predetermined format.

awk 'BEGIN { print strftime()  }'

The output will look like this:

Wed Dec 29 17:28:32 EST 2021

You can aslo specify the format like this if you want:

awk 'BEGIN { print strftime("Time = %m/%d/%Y %H:%M:%S", systime())  }'

The output will look more like this:

Time = 12/29/2021 17:29:05

These time and date functions can be used:

systime() seconds since epoc
mktime(“2018 11 19 32 15 05”) seconds since epoc based on datespec string
strftime(“Time = %m/%d/%Y %H:%M:%S”, systime()) convert seconds since epoc to timestamp

These can be used for constructiong date format strings:

%a weekday - short
%A weekday - full
%b month - short
%B month full
%c date / time by locale
%C century
%d day of month
%D %m/%d/%y.
%e day of month ( padded )
%F %Y-%m-%d ISO 8601 standard date format
%g week of the year ( goes past end of year )
%G full year of week number as decimal
%h same as %b.
%H hour (00–23)
%I hour (01–12)
%j day of the year
%m month of the year
%M minute
%n newline char
%p AM/PM
%r 12-hour clock time for locale
%R %H:%M.
%S second
%t just a tab char
%T %H:%M:%S.
%u weekday (1–7). Monday is is 1
%U week number (00–53) starting on Sunday
%V week number (01–53) starting on Monday
%w weekday starting on Sunday (0–6)
%W week number (00–53) starting on Monday
%x date formated for locale
%X time formated for locale
%y year modulo 100 (00–99)
%Y year - full
%z time-zone offset in +HHMM format
%Z time zone name