How To Display The Date And Time Using Linux Command Line

Display The Date And Time Using Linux
Display The Date And Time Using Linux.

Introduction

In this guide I will show you how to print the date and time using the Linux command line in various formats.

How To Display The Date And Time

You could probably have guessed the command to display the date and time using the Linux command line. It is quite simply this:

date

By default the output will be something like this:

Wed Apr 20 19:19:21 BST 2016

You can get the date to display any or all of the following elements:

  • %a - abbreviated day name (i.e. mon, tue, wed)
  • %A - full day name (i.e. Monday, Tuesday, Wednesday)
  • %b or %h - abbreviated month name (i.e. jan, feb, mar)
  • %B - full month name (January, February, March)
  • %c - locales date and time (full date and time)
  • %C - short year (i.e 14, 15, 16)
  • %d - day of month (i.e. 01, 02, 03)
  • %D - same as M/D/Y (i.e. 04/20/16)
  • %e - day of month padded (i.e. ' 1', ' 2')
  • %F - full date, same as yyyy-mm-dd
  • %H - hour (00, 01, 02, 21, 22, 23)
  • %I - hour (1,2,3,10,11,12)
  • %j - day of year (i.e. 243)
  • %k - hour padded (i.e. '1' becomes ' 1')
  • %l - hour padded (12 hour clock)
  • %m - month number (1,2,3)
  • %M - minute (1,2,3,57,58,59)
  • %n - new line
  • %N - nanoseconds
  • %p - AM or PM
  • %P - like %p but lowercase (ironically) 
  • %r - locales 12 hour clock time
  • %R - 24 hour version of hour and minute
  • %s - seconds since 1970-01-01 00:00:00
  • %S - second (01,02,03, 57, 58, 59)
  • %t - a tab
  • %T - time same as %H:%M:%S
  • %u - day of week (1 is Monday, 2 is Tuesday etc)
  • %U - week number of year (assuming Sunday as first day of the week)
  • %V - ISO week number with Monday as the first day of the week
  • %w - day of week (0 is Sunday)
  • %W - week number of the year with Monday as the first day of the week
  • %x - locales date representation (12/31/2015)
  • %X - locales time representation (14:44:44)
  • %y - last two digits of year
  • %Y - year
  • %z - numeric time zone (i.e. -0400)
  • %:z - numeric time zone as follows (i.e. -04:00)
  • %::z - numeric time zone as follows (i.e. -04:00:00)
  • %Z - alphabetic time zone abbreviation (GMT) 
  • - - a single hyphen prevents zero padding
  • _ - a single underscore pads with spaces
  • 0 - pads with zeroes
  • ^ - use uppercase if possible
  • # - use opposite case if possible

That is a huge number of options and I suspect the date command is the one most people try to add something to when they first want to contribute to Linux and compile their first program.

Essentially if you want to display just the time you can use the following:

date +%T

This will output 19:45:00. (i.e. hours, minutes then seconds)

You can also achieve the above by using the following:

date +%H:%M:%S

You can attach the date as well using the above command:

date +%d/%m/%Y%t%H:%M:%S

Basically you can use any combination of the above switches after the plus symbol to output the date as you so wish. If you want to add spaces you can use quotes around the date.

date +'%d/%m/%Y %H:%M:%S'

How To Show The UTC Date

You can view the UTC date for your computer using the following command:

date -u

If you are in the UK you will notice that instead of showing "18:58:20" as the time it will show "17:58:20" as the time.

How To Show The RFC Date

You can view the RFC date for your computer using the following command:

date -r

This displays the date in the following format:

Wed, 20 Apr 2016 19:56:52 +0100

This is useful as it shows that you are an hour ahead GMT.

Some Useful Date Commands

Do you want to know the date next Monday? Try this out:

date -d "next Monday"

At the point of writing this returns "Mon 25 Apr 00:00:00 BST 2016"

The -d basically prints a date in the future.

Using the same command you can find out which day of the week your birthday or Christmas is on.

date -d 12/25/2016

The result is Sun Dec 25. 

Summary

It is worth checking out the manual page for the date command using the following command:

man date