Apr 292012

The Linux date command is one simple command that I can never seem to get the hang of. Just changing the date takes me about ten minutes of searching the man page to find out the correct datestring format. NTP has made me forgetful, I guess.

Here are a few commonly occurring date command usages:

I’ll recount the details of each particular nugget below.

Manually set the date

Here’s the one that always give me trouble – I can never remember the format string, the order of the digits, or whether “month” or “minute” is the upper-case “M”. There’s three easy ways to do it, really. Let’s assume I’m setting it to 2.15pm, 26th April 2012

  1. Get the current date string and then just copy the same format text after the “-s” switch:
       # date
      Sun Apr 29 18:29:31 BST 2012
       # date -s "Sun Apr 26 14:15:00 BST 2012"
      Sun Apr 26 14:15:00 BST 2012
  2. Use the default format order: %d%m%H%M%y (day, month, hour, minute, year). This is shorter, but hard to remember exactly.
       # date 0426141512
      Thu Apr 26 14:15:00 BST 2012
  3. Specify a date format with the “+” sign:
       # date +%y%m%d%H%M "1204261415"

In the examples above, %y is the two-digit year. %Y is the full four-digit year.

Change date only, leaving time unchanged

If you were to attempt to change the date only, omitting the time from the format string, like this (changing the date to the 11th March):

  # date +%y%m%d -s 120311
Mon Mar 11 00:00:01 GMT 2012

The problem is that the time gets set to midnight. In order to leave the time unchanged, use a shell command like this.

The date and time is:

Mon Apr 19 22:05:58 GMT 2012

To change the day to 11th March, leaving time as is:

  # date 0311`date "+%H%M.%S"`
 Sun Mar 11 22:05:59 GMT 2012

Convert Epoch time to human readable form

Some applications, in their log files, use Epoch time (measured in the number of seconds since 1st January 1970). I’m looking at you, Nagios. It’s only useful in as far as you can easily calculate time differences from one datestamp to the next by simply subtraction. But reading it is a nuisance.

Here’s the easy way to convert:

  # date -d '@1331504087'
  Sun Mar 11 22:14:47 GMT 2012

Display the date of yesterday

It used to be a difficult thing to return yesterday’s date, although newer versions of the date command make this easy. This is what I love about Linux – its constant revisions which improve its functionality.

It’s as easy as this:

  # date -d '1 day ago' +'%d/%m/%Y'

Once upon a time, Perl was the easiest way to do this, and I’ll repeat the method here for the purpose of completeness, and so I can finally throw away my notebook.

   # perl -e 'printf "%s\n", scalar localtime(time-86400)'

So the date command is pretty simple, really. The key things are the “%m” is “month”, and “%M” is minute. The “-s” flag sets the time, and the “+” sign specifies the format that either the input or output is using.

Matt Parsons is a freelance Linux specialist who has designed, built and supported Unix and Linux systems in the finance, telecommunications and media industries.

He lives and works in London.

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>