Cyberithub

11 Useful Linux date command examples (How to set date and time in Linux)

In this article, I will take you through 11 Useful Linux date command examples. date is an important Linux command frequently used to set System date and time. You might have encountered few cluster based set up where each and every node needs to be in date and time sync with each other to be able to run successfully. Once it goes out of date or time sync beyond few seconds or minutes, cluster goes down. Hence it is very important to set the date and time correctly in the System. In this session we will look at how to set date and time in Linux using date command examples.

Syntax

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

11 Useful Linux date command examples (How to set date and time in Linux) 1

Linux date command Examples

Also Read: 13 Useful Examples of touch command in Linux

Example 1: How to check current date and time using date command

If you want to check current date and time of your System then you need to simple run date command as shown below.

[root@localhost ~]# date
Fri May 15 19:16:12 EDT 2020

NOTE:

Please note that here I am using root user to run all the below commands.You can use any user with sudo access to run all these commands. For more information Please check Step by Step: How to Add User to Sudoers to provide sudo access to User.

Example 2: How to check future date using date command

If you want to check date of some future day then you can check it by using --date option.

[root@localhost ~]# date --date="next fri"
Fri May 22 00:00:00 EDT 2020

To check Next Tueday date you can use below command.

[root@localhost ~]# date --date="next tue"
Tue May 19 00:00:00 EDT 2020

To check Next Sunday date you can use below command.

[root@localhost ~]# date --date="next sun"
Sun May 24 00:00:00 EDT 2020

To check tomorrow date you can use below command.

[root@localhost ~]# date --date="tomorrow"
Mon May 18 23:14:17 EDT 2020

To check yesterday date you can use below command.

[root@localhost ~]# date --date="yesterday"
Sat May 16 23:14:22 EDT 2020

To check after 2 Years date you can use below command.

[root@localhost ~]# date --date="2 Year"
Tue May 17 23:14:29 EDT 2022

To check after 2 months date you can use below command.

[root@localhost ~]# date --date="2 months"
Fri Jul 17 23:14:48 EDT 2020

–date : display time described by STRING, not ‘now’. More info on date command Man page.

Example 3: How to check version of date command

If you want to check the version of date command then you need to use --version option as shown below. As you can see from below output current version is 8.22

[root@localhost ~]# date --version
date (GNU coreutils) 8.22
Copyright (C) 2013 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.

–version : output version information and exit. More info on date command Man page.

Example 4: How to Check current UTC Time using date command

You can check the current Coordinated Universal Time by using date -u command.

[root@localhost ~]# date -u
Fri May 15 23:25:48 UTC 2020

-u : print or set Coordinated Universal Time (UTC)

Example 5: How to Set date and time in Linux

If you want to change both data and time then you can use --set option with date command to change that as shown below.

[root@localhost ~]# date --set="2020-05-17 11:12:13"
Sun May 17 11:12:13 EDT 2020

–set : set time described by STRING

Example 6: How to Set only time in Linux using date command

If you want to only change the time then you can use the same --set option as shown in above example and set the time only instead of setting both date and time.

[root@localhost ~]# date --set="14:12:13"
Sun May 17 14:12:13 EDT 2020

Example 7: How to Set only date in Linux using date command

If you want to change only date but not the time then you need to first check the current time and then set accordingly. For example, if you check the current date and time you can see that current date is 16th May 2020 and current time is 13:16:04 EDT.

[root@localhost ~]# date
Sat May 16 13:16:04 EDT 2020

So, if you want to change the date only say to 17 May,2020 in this example keeping the time same then you need to use below date command.

[root@localhost ~]# date --set="2020-05-17 13:16:04"
Sun May 17 13:16:04 EDT 2020

Example 8: How to Check the Last Modification date timestamp of a file

If you want to check the last modification date timestamp of a file then you need to use -r option with date command as shown below. In this example we are checking the last modification date timestamp of a file file1.txt using date -r file1.txt command.

[root@localhost ~]# date -r file1.txt
Tue Apr 28 00:55:56 EDT 2020

-r : display the last modification time of FILE

Example 9: How to Display date in RFC Format

If you want to display date in RFC format then you need to use date -R command as shown below.

[root@localhost ~]# date -R
Sun, 17 May 2020 14:13:05 -0400

-R : output date and time in RFC 2822 format

Example 10: Using Format Specifiers with date command

There are lot of format specifiers available to use with date command as per the needs. You can check and find its usages below.

[root@localhost ~]# date "+%a"
Sun

%a locale’s abbreviated weekday name (e.g., Sun)

[root@localhost ~]# date "+%A"
Sunday

%A locale’s full weekday name (e.g., Sunday)

[root@localhost ~]# date "+%b"
May

%b locale’s abbreviated month name (e.g., Jan)

[root@localhost ~]# date "+%B"
May

%B locale’s full month name (e.g., January)

[root@localhost ~]# date "+%c"
Sun 17 May 2020 04:50:02 PM EDT

%c locale’s date and time (e.g., Thu Mar 3 23:05:25 2005)

[root@localhost ~]# date "+%C"
20

%C century; like %Y, except omit last two digits (e.g., 20)

[root@localhost ~]# date "+%d"
17

%d day of month (e.g., 01)

[root@localhost ~]# date "+%D"
05/17/20

%D date; same as %m/%d/%y

[root@localhost ~]# date "+%e"
17

%e day of month, space padded; same as %_d

[root@localhost ~]# date "+%F"
2020-05-17

%F full date; same as %Y-%m-%d

[root@localhost ~]# date "+%g"
20

%g last two digits of year of ISO week number (see %G)

[root@localhost ~]# date "+%G"
2020

%G year of ISO week number (see %V); normally useful only with %V

[root@localhost ~]# date "+%h"
May

%h same as %b

[root@localhost ~]# date "+%H"
16

%H hour (00..23)

[root@localhost ~]# date "+%I"
04

%I hour (01..12)

[root@localhost ~]# date "+%j"
138

%j day of year (001..366)

[root@localhost ~]# date "+%k"
21

%k hour, space padded ( 0..23); same as %_H

[root@localhost ~]# date "+%l"
9

%l hour, space padded ( 1..12); same as %_I

[root@localhost ~]# date "+%m"
05

%m month (01..12)

[root@localhost ~]# date "+%M"
14

%M minute (00..59)

[root@localhost ~]# date "+%n"

%n a newline

[root@localhost ~]# date "+%N"
046197179

%N nanoseconds (000000000..999999999)

[root@localhost ~]# date "+%p"
PM

%p locale’s equivalent of either AM or PM; blank if not known

[root@localhost ~]# date "+%P"
pm

%P like %p, but lower case

[root@localhost ~]# date "+%r"
09:15:07 PM

%r locale’s 12-hour clock time (e.g., 11:11:04 PM)

[root@localhost ~]# date "+%R"
21:15

%R 24-hour hour and minute; same as %H:%M

[root@localhost ~]# date "+%s"
1589764531

%s seconds since 1970-01-01 00:00:00 UTC

[root@localhost ~]# date "+%S"
45

%S second (00..60)

[root@localhost ~]# date "+%t"

%t a tab

[root@localhost ~]# date "+%T"
21:16:07

%T time; same as %H:%M:%S

[root@localhost ~]# date "+%u"
7

%u day of week (1..7); 1 is Monday

[root@localhost ~]# date "+%U"
20

%U week number of year, with Sunday as first day of week (00..53)

[root@localhost ~]# date "+%V"
20

%V ISO week number, with Monday as first day of week (01..53)

[root@localhost ~]# date "+%w"
0

%w day of week (0..6); 0 is Sunday

[root@localhost ~]# date "+%W"
19

%W week number of year, with Monday as first day of week (00..53)

[root@localhost ~]# date "+%x"
05/17/2020

%x locale’s date representation (e.g., 12/31/99)

[root@localhost ~]# date "+%y"
20

%y last two digits of year (00..99)

[root@localhost ~]# date "+%Y"
2020

%Y year

[root@localhost ~]# date "+%z"
-0400

%z +hhmm numeric time zone (e.g., -0400)

Example 11: Check Other date command options

If you want to check all the other options available with date command you can use date --help command as shown below.

[root@localhost ~]# 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.

Mandatory arguments to long options are mandatory for short options too.
-d, --date=STRING display time described by STRING, not 'now'
-f, --file=DATEFILE like --date once for each line of DATEFILE
-I[TIMESPEC], --iso-8601[=TIMESPEC] output date/time in ISO 8601 format.
TIMESPEC='date' for date only (the default),
'hours', 'minutes', 'seconds', or 'ns' for date
and time to the indicated precision.
-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 (UTC)
--help display this help and exit
--version output version information and exit

–help : display this help and exit

 

Recommended Posts:-

Understanding Kafka Console Producer and Consumer in 10 Easy Steps

Popular firewalld examples to open a port on RedHat/CentOS 7

8 Most Popular mkdir command in Linux with Examples

26 Useful Firewall CMD Examples on RedHat/CentOS 7

12 Most Popular rm command in Linux with Examples

9 useful w command in Linux with Examples

Popular Apache Kafka Architecture Explained Using 4 Basic Components

5 Easy Steps to recover LVM2 Partition , PV , VG , LVM metadata in Linux

Leave a Comment