The date command in Linux boxes is one of the most powerful open source utilities. It is not just for setting the clock on your PC or server, or showing you what the current time is, it can do amazingly more. It can virtually answer all of your chronological questions.
The simplest use case of date command is to view current time, possibly in different time formats –
$ date Sat Dec 17 00:45:35 EST 2016 $ date '+%Y-%m-%d' 2016-12-17 $ date '+%c' Sat 17 Dec 2016 12:45:51 AM EST
It is useful in converting time to/from epoch as well –
$ date '+%s' 1481953669 $ date --date='@1481953669' Sat Dec 17 00:47:49 EST 2016
The most user friendly use case of the date command is the ‘-d’ or ‘–date’ options, which accepts free format human readable date string such as “yesterday”, “last week”, “next year”, “3 min ago”, “last friday + 2 hours” etc. Here is an excerpt from the man page of the GNU date command –
DATE STRING
The --date=STRING is a mostly free format human readable date string such as "Sun, 29 Feb 2004 16:21:42 -0800" or "2004-02-29 16:21:42" or even "next Thursday". A date string may con?
tain items indicating calendar date, time of day, time zone, day of week, relative time, relative date, and numbers. An empty string indicates the beginning of the day. The date
string format is more complex than is easily documented here but is fully described in the info documentation.
Let us play with it –
$ date -d '2 hours ago' Fri Dec 16 22:51:25 EST 2016 $ date -d '2 hours ago' '+%c' Fri 16 Dec 2016 10:51:30 PM EST $ env TZ=America/Los_Angeles date -d '2 hours ago' '+%c' Fri 16 Dec 2016 07:52:33 PM PST $ date -d 'jan 2 1990' Tue Jan 2 00:00:00 EST 1990 $ date -d 'yesterday' Fri Dec 16 00:53:04 EST 2016 $ date -d 'next year + 2 weeks' Sun Dec 31 00:53:27 EST 2017
To give a practical example, let us use the date command to get, on which day all the birth days of someone fall, given their date of birth. This can be for past birth days as well as the future. For this example, we will do it from date of birth to this date. Let us pick someone who was born on Feb 29, 1988. This is an edge case. The date command should be smart enough to figure out the leap years.
for year in {1988..2016}; do date -d "feb 29 $year" &>/dev/null if [ $? -eq 0 ]; then echo -n "Year: $year " ; date -d "feb 29 $year" '+%c' fi done Year: 1988 Mon 29 Feb 1988 12:00:00 AM EST Year: 1992 Sat 29 Feb 1992 12:00:00 AM EST Year: 1996 Thu 29 Feb 1996 12:00:00 AM EST Year: 2000 Tue 29 Feb 2000 12:00:00 AM EST Year: 2004 Sun 29 Feb 2004 12:00:00 AM EST Year: 2008 Fri 29 Feb 2008 12:00:00 AM EST Year: 2012 Wed 29 Feb 2012 12:00:00 AM EST Year: 2016 Mon 29 Feb 2016 12:00:00 AM EST
A typical case would be, say for someone born on Jan 8 1990 –
age=0 for year in {1990..2016}; do echo -n "Age: $age "; date -d "Jan 8 $year" '+%A %d %B %Y' age=$((age+1)) done Age: 0 Monday 08 January 1990 Age: 1 Tuesday 08 January 1991 Age: 2 Wednesday 08 January 1992 Age: 3 Friday 08 January 1993 Age: 4 Saturday 08 January 1994 Age: 5 Sunday 08 January 1995 Age: 6 Monday 08 January 1996 Age: 7 Wednesday 08 January 1997 Age: 8 Thursday 08 January 1998 Age: 9 Friday 08 January 1999 Age: 10 Saturday 08 January 2000 Age: 11 Monday 08 January 2001 Age: 12 Tuesday 08 January 2002 Age: 13 Wednesday 08 January 2003 Age: 14 Thursday 08 January 2004 Age: 15 Saturday 08 January 2005 Age: 16 Sunday 08 January 2006 Age: 17 Monday 08 January 2007 Age: 18 Tuesday 08 January 2008 Age: 19 Thursday 08 January 2009 Age: 20 Friday 08 January 2010 Age: 21 Saturday 08 January 2011 Age: 22 Sunday 08 January 2012 Age: 23 Tuesday 08 January 2013 Age: 24 Wednesday 08 January 2014 Age: 25 Thursday 08 January 2015 Age: 26 Friday 08 January 2016