Monday, June 11, 2012

How to format date for display or to use in a shell script

SkyHi @ Monday, June 11, 2012
echo "[potato2/check_relay] $HOSTNAME - `date` " > /tmp/.check_relay.txt
grep 'check_relay' /var/www/Sendmaillog/maillog |grep "`date "+%b %d"`"| awk '{print $7}'|sort | uniq -c |sort -nr >> /tmp/.check_relay.txt
head -500 /tmp/.check_relay.txt  > /tmp/.check_relay.txt

mail -s "[potato/check_relay] $HOSTNAME - `date`" < /tmp/.check_relay.txt

Q. How do I format date to display on screen on for my scripts as per my requirements?
A. You need to use standard date command to format date or time for output or to use in a shell script.
Syntax to specify format
date +FORMAT

Task: Display date in mm-dd-yy format

Type the command as follows:
$ date +"%m-%d-%y"
Turn on 4 digit year display:
$ date +"%m-%d-%Y"
Just display date as mm/dd/yy format:
$ date +"%D"

Task: Display time only

Type the command as follows:
$ date +"%T"
Display locale’s 12-hour clock time
$ date +"%r"
07:56:05 PM
Display time in HH:MM format:
$ date +"%H-%M"

How do I save time/date format to a variable?

Simply type command as follows at a shell prompt:
$ NOW=$(date +"%m-%d-%Y")
To display a variable use echo / printf command:
$ echo $NOW
Sample shell script:
NOW=$(date +"%m-%d-%Y")
# rest of script

Complete list of FORMAT control characters supported by date command

FORMAT controls the output.It can be the combination of any one of the following:


a literal %

locale's abbreviated weekday name (e.g., Sun)
locale's full weekday name (e.g., Sunday)

locale's abbreviated month name (e.g., Jan)

locale's full month name (e.g., January)

locale's date and time (e.g., Thu Mar 3 23:05:25 2005)

century; like %Y, except omit last two digits (e.g., 21)

day of month (e.g, 01)
date; same as %m/%d/%y

day of month, space padded; same as %_d

full date; same as %Y-%m-%d

last two digits of year of ISO week number (see %G)

year of ISO week number (see %V); normally useful only with %V

same as %b
hour (00..23)

hour (01..12)

day of year (001..366)

hour ( 0..23)

hour ( 1..12)

month (01..12)
minute (00..59)

a newline

nanoseconds (000000000..999999999)

locale's equivalent of either AM or PM; blank if not known

like %p, but lower case

locale's 12-hour clock time (e.g., 11:11:04 PM)
24-hour hour and minute; same as %H:%M

seconds since 1970-01-01 00:00:00 UTC

second (00..60)

a tab

time; same as %H:%M:%S

day of week (1..7); 1 is Monday
week number of year, with Sunday as first day of week (00..53)

ISO week number, with Monday as first day of week (01..53)

day of week (0..6); 0 is Sunday

week number of year, with Monday as first day of week (00..53)

locale's date representation (e.g., 12/31/99)

locale's time representation (e.g., 23:13:48)
last two digits of year (00..99)


+hhmm numeric timezone (e.g., -0400)

+hh:mm numeric timezone (e.g., -04:00)

+hh:mm:ss numeric time zone (e.g., -04:00:00)

numeric time zone with : to necessary precision (e.g., -04, +05:30)

alphabetic time zone abbreviation (e.g., EDT)

See also: