date command with 9 practical examples

In This article we are going to see Linux date command with examples, basic Linux command.

date command is used to print or set the system date and time.

SYNOPSIS

date [OPTION]... [+FORMAT]
date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]

Summary

  • Using date command we can print date along with time
  • Display date & time from a given string
  • Read date from a file
  • Display Universal time
  • Set system date and time using date command

1. Display date & time from a given string

date command will take an input and it will display the given string using –date option. As shown in below example $date –date=”Month/Date/Year”.

[root@arkit71 ~]# date --date="1/31/2016"
Sun Jan 31 00:00:00 EST 2016

[root@arkit71 ~]# date --date="31 Jan 2016"
Sun Jan 31 00:00:00 EST 2016

[root@arkit71 ~]# date --date="Jan 2 2016"
Sat Jan 2 00:00:00 EST 2016

2. Read date from a file using –file option

we can execute multiple lines if input from a file to date command.

Create a file and write few date formats in that and execute date command with –file option

# touch dateinfile

[root@arkit71 ~]# cat dateinfile
Jan 31 2016
31 Jan 2016

[root@arkit71 ~]# date --file=dateinfile
Sun Jan 31 00:00:00 EST 2016
Sun Jan 31 00:00:00 EST 2016

3. Display epoch time with difference of 1970-01-01 UTC

epoch time: Unix time (also known as POSIX time or Epoch time) is a system for describing instants in time, defined as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970, not counting leap seconds.

It is used widely in Unix-like and many other operating systems and file formats. Because it does not handle leap seconds, it is neither a linear representation of time nor a true representation of UTC. Unix time may be checked on most Unix systems by typing date +%s on the command line.

[root@arkit71 ~]# date --date=@10
Wed Dec 31 19:00:10 EST 1969
[root@arkit71 ~]# date
Sun Jan 31 10:35:16 EST 2016
[root@arkit71 ~]# date --date=@60
Wed Dec 31 19:01:00 EST 1969
[root@arkit71 ~]# date --date=@120
Wed Dec 31 19:02:00 EST 1969

4. Convert actual date & time to epoch and epoch to actual date & time (human readable)

using +%s option we can print current actual date & time to epoch time format. Logs will store in log files with epoch time format because its very compact format compare to actual date & time

[root@arkit71 ~]# date
Sun Jan 31 10:45:22 EST 2016

[root@arkit71 ~]# date +%s    <<-- Printing current date to epoch format
1454255124

[root@arkit71 ~]# date -d @1454255124  <<-- converting epoch time format to actual human readable format
Sun Jan 31 10:45:24 EST 2016

[root@arkit71 ~]# date +%s -d"2016-01-31"    <<-- converting actual date to epoch format
1454216400

[root@arkit71 ~]# date -d @1454216400
Sun Jan 31 00:00:00 EST 2016

5. Display past date

We can also give an different format of input to print past date like “yesterday” “1 year ago”. Let’s see an few examples shown below.

[root@arkit71 ~]# date --date='3 seconds ago'
Sun Jan 31 10:59:48 EST 2016

[root@arkit71 ~]# date --date='1 day ago'
Sat Jan 30 10:59:57 EST 2016

[root@arkit71 ~]# date --date='yesterday'
Sat Jan 30 11:00:04 EST 2016

[root@arkit71 ~]# date --date='1 month ago'
Thu Dec 31 11:00:13 EST 2015

[root@arkit71 ~]# date --date='1 year ago'
Sat Jan 31 11:00:21 EST 2015

[root@arkit71 ~]# date --date='2 years ago'
Fri Jan 31 11:00:28 EST 2014

[root@arkit71 ~]# date --date='tomorrow'
Mon Feb 1 11:01:35 EST 2016

6. Changing system date & time using –set option

We can also change the system date and time using ‘-s’ option

[root@arkit71 ~]# date     <<-- Current Date
Tue Feb 2 04:25:37 EST 2016
[root@arkit71 ~]# date -s "Sun Jan 31 08:05:29 EST 2016"   <<-- Changed to 31st Jan 2016
Sun Jan 31 08:05:29 EST 2016

[root@arkit71 ~]# date    <<-- After date & time change
Sun Jan 31 08:05:31 EST 2016
[root@arkit71 ~]# date -s "Tue Feb 2 04:25:37 EST 2016"  <<-- Again changed back to 2nd feb 2016
Tue Feb 2 04:25:37 EST 2016
[root@arkit71 ~]# date
Tue Feb 2 04:25:38 EST 2016

7. Print universal time using option -u

Universal Time (UT) is a time standard based on Earth’s rotation. It is a modern continuation of Greenwich Mean Time (GMT),  i.e. the mean solar time on the Prime Meridian at Greenwich.

[root@arkit71 ~]# date
Tue Feb 2 04:29:59 EST 2016

[root@arkit71 ~]# date -u
Tue Feb 2 09:30:01 UTC 2016

8. Display the file modified time stamp using -r option

Using date command we can also verify the file modified date option is ‘-r’

[root@arkit71 ~]# date -r kumar
Wed Jan 27 12:13:11 EST 2016

[root@arkit71 ~]# echo "date modify" > kumar   <<-- Modified the file

[root@arkit71 ~]# date -r kumar  <<-- After file modifitcaion
Tue Feb 2 04:32:32 EST 2016

9. Convert given date & time to current time zone

what every the date & time you can specify using ‘-d’ & ‘–date’ then it will convert to system time zone

[root@arkit71 ~]# date -d '2016-06-24 18:00 PDT'
Fri Jun 24 21:00:00 EDT 2016

[root@arkit71 ~]# date --date='TZ="Africa/Nairobi" 18:00'
Tue Feb 2 10:00:00 EST 2016

10. Print Custom date & time as per your requirement

As we can also print the date & time as per our requirement

[root@arkit71 ~]# date +"%M-%d-%y %H:%M:%S"
41-02-16 04:41:15

FORMAT controls the output. Interpreted sequences are:

%% a literal %
%a locale's abbreviated weekday name (e.g., Sun)
%A locale's full weekday name (e.g., Sunday)
%b locale's abbreviated month name (e.g., Jan)
%B locale's full month name (e.g., January)
%c locale's date and time (e.g., Thu Mar 3 23:05:25 2005)
%C century; like %Y, except omit last two digits (e.g., 20)
%d day of month (e.g., 01)
%D date; same as %m/%d/%y
%e day of month, space padded; same as %_d
%F full date; same as %Y-%m-%d
%g last two digits of year of ISO week number (see %G)
%G year of ISO week number (see %V); normally useful only with %V
%h same as %b
%H hour (00..23)
%I hour (01..12)
%j day of year (001..366)
%k hour, space padded ( 0..23); same as %_H
%l hour, space padded ( 1..12); same as %_I
%m month (01..12)
%M minute (00..59)
%n a newline
%N nanoseconds (000000000..999999999)
%p locale's equivalent of either AM or PM; blank if not known
%P like %p, but lower case
%r locale's 12-hour clock time (e.g., 11:11:04 PM)
%R 24-hour hour and minute; same as %H:%M
%s seconds since 1970-01-01 00:00:00 UTC
%S second (00..60)
%t a tab
%T time; same as %H:%M:%S
%u day of week (1..7); 1 is Monday
%U week number of year, with Sunday as first day of week (00..53)
%V ISO week number, with Monday as first day of week (01..53)
%w day of week (0..6); 0 is Sunday
%W week number of year, with Monday as first day of week (00..53)
%x locale's date representation (e.g., 12/31/99)
%X locale's time representation (e.g., 23:13:48)
%y last two digits of year (00..99)
%Y year
%z +hhmm numeric time zone (e.g., -0400)
%:z +hh:mm numeric time zone (e.g., -04:00)
%::z +hh:mm:ss numeric time zone (e.g., -04:00:00)
%:::z numeric time zone with : to necessary precision (e.g., -04, +05:30)
%Z alphabetic time zone abbreviation (e.g., EDT)
By default, date pads numeric fields with zeroes. The following optional flags may follow '%':
- (hyphen) do not pad the field
_ (underscore) pad with spaces
0 (zero) pad with zeros
^ use upper case if possible
# use opposite case if possible

After any flags comes an optional field width, as a decimal number; then an optional modifier, which is either E to use the locale’s alternate representations if available, or O to use the locale’s alternate numeric symbols if available.

Video Playlist for the same

Please comment your feedback on the same…

Related Articles

ls command  with 25 practical examples

25 commonly used Linux commands

Thanks for your wonderful Support and Encouragement