date Command in Linux with Examples

date Command

The date command helps to view and set the system date and time in Linux machine. The date command allows you to see the several formats in date and time.

Syntax

date < options> < formats>

Display the date and time

The date command without using any option it will show you the system date and time. The day in abbreviate and month also abbreviates date in number and time format HH:MM:SS in IST and last in year.

Example

[root@linuxhelp ~]# date 

Thu Feb 26 14:47:59 EST 2016

Display the date and time in specified format

The date command with +DATE this show you date in MM/DD/YY in the line one. The date command with using TIME this show you date in HH:MM:SS in the line second.

Example

[root@linuxhelp ~]# date " +DATE: %m/%d/%y%nTIME: %H:%M:%S"  
DATE: 02/04/16
TIME: 15:07:17

Display the date and time using -r option

The date command with -R command will display the date and time in RFC 2822 format.

Example

[root@linuxhelp ~]# date -R 
Thu, 04 Feb 2016 15:18:45 -0500

Display the date and time using -u option

The date command with -u option will display the date and time in coordinated universal time (UTC) time format.

Example

[root@linuxhelp ~]# date -u 
Thu Feb 4 20:25:15 UTC 2016

Display date from string value

The date command with --date option will display the string value in date. You can give string value in many format like DD/MM/YYYY or in string format “ Mar 04 16” .

Example

[root@linuxhelp ~]# date --date=" 03/2/16"  
Wed Mar 2 00:00:00 EST 2016

Display the date and time in string type

The date command with --date option will display the string value in date and also the time.

The before command will show only the date in string format but this command will display time also.

Example

[root@linuxhelp ~]# date --date=" Feb 4 2016 13:12:10"  
Thu Feb 4 13:12:10 EST 2016

Display the relative date using -date option

This date command with using --date option will display the next on day of date. This is called Relative values it can use for see the future date.

Example

[root@linuxhelp ~]# date --date=" next mon"  
Mon Feb 8 00:00:00 EST 2016

Display the date in convert seconds since the epoch (1970-01-01 utc) to a date

If string=@is given to date command then the date command convert seconds since the epoch (1970-01-01 UTC) to a date. If you want convert second in 30 you can run this command (date &ndash date=@30).

Example

[root@linuxhelp ~]# date --date=@10 
Wed Jan 01 13:00:10 BST 1970

Display the date in past date

In this command will help you on see the time in 3 seconds before what time in. This command will help you also you can see the past date and time.

Example
The current time is

[root@linuxhelp ~]#date
Thu Feb 4 16:51:49 SST 2016
[root@linuxhelp ~]# date --date=' 3 seconds ago' 
Thu Feb 4 16:51:46 SST 2016
[root@linuxhelp ~]# date --date=" 1 day ago" 
Tue Feb 3 16:58:37 SST 2016
[root@linuxhelp ~]# date --date=" yesterday" 
Wed Feb 3 16:59:38 SST 2016
[root@linuxhelp ~]# date --date=" 1 month ago" 
Wed Jan 4 17:00:46 SST 2016
[root@linuxhelp ~]# date --date=" 1 year ago" 
Thu Feb 4 17:01:43 SST 2015

Set date and time to using -set

You can set date and time of your system using -s or &ndash set option as shown below.
First check the current date using date command.

This command works only the root user.

Example

[root@linuxhelp ~]# date 
Thu Feb 4 17:07:01 SST 2016
And set the Date using set option.

[root@linuxhelp ~]# date -s " Sun May 20 21:00:00 PDT 2017"  
And check the date again.

[root@linuxhelp ~]# date  
Thu Feb 4 17:07:01 SST 2016
Sat May 20 17:00:00 SST 2017

Display the date version

This date command with --version or -V option will display the version details.

Example

[root@linuxhelp ~]# date -&ndash version  
date (GNU coreutils) 8.4
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later < http://gnu.org/licenses/gpl.html> .
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by David MacKenzie.

To display the date help

This date command with --help option will display the user manual.

Example

[root@linuxhelp ~]# date --help  
Usage: date [OPTION]... [+FORMAT]
  or:  date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
Display the current time in the given FORMAT, or set the system date.

  -d, --date=STRING         display time described by STRING, not `now' 
  -f, --file=DATEFILE       like --date once for each line of DATEFILE
  -r, --reference=FILE      display the last modification time of FILE
  -R, --rfc-2822            output date and time in RFC 2822 format.
                            Example: Mon, 07 Aug 2006 12:34:56 -0600
      --rfc-3339=TIMESPEC   output date and time in RFC 3339 format.
                            TIMESPEC=`date' , `seconds' , or `ns'  for
                            date and time to the indicated precision.
                            Date and time components are separated by
                            a single space: 2006-08-07 12:34:56-06:00
  -s, --set=STRING          set time described by STRING
  -u, --utc, --universal    print or set Coordinated Universal Time
      --help     display this help and exit
      --version  output version information and exit

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 ( 0..23)
  %l   hour ( 1..12)
  %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 timezone (e.g., -0400)
  %:z  +hh:mm numeric timezone (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.

Report date bugs to bug-coreutils@gnu.org
GNU coreutils home page: < http://www.gnu.org/software/coreutils/> 
General help using GNU software: < http://www.gnu.org/gethelp/> 
For complete documentation, run: info coreutils ' date invocation' 

Tag : date command
FAQ
Q
How do I save time/date format to the shell variable?
A
Simply type the following command at the shell prompt:$ NOW=$(date +"%m-%d-%Y")To display a variable use echo / printf command:$ echo $NOW.
Q
How do I view and set date under UNIX operating systems? How do I see the current time/date on Unix based server?
A
The date command under UNIX displays date and time. You can use the same command set date and time. You must be the super-user (root) to change the date and time on Unix like operating system
Q
How to retrieve Linux or Unix bash command line history by date and time?
A
You can use the following command to retrieve Linux or Unix bash command line history by date and time,Syntax: #history.
Q
HOw to set the current Date and time using the "date" command?
A
You can use the following command to set the current Date and Time using the "date" command in Linux. For Ex: "date --set="Tue Nov 13 15:23:34 PDT 2018".
Q
How to check the today date in Linux?
A
You can use the following command to check the today Date in Linux. For Ex: "date -d now".