How to print a date or set system date and time in linux

Date command in linux

date – print or set the system date and time

2. Date command Synopsis in linux

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

3. Frequently used options date command

-u, --utc, --universal
      print or set Coordinated Universal Time
+%F     full date; same as %Y-%m-%d 
4. Examples date command

date command is used to set and display time. By typing date command without any options and arguments the date command will display a current time:$ date
Wed May 12 12:30:55 EST 2010

date command can also be used to set time. However, this requires superuser permissions. To set time to 20th March 2010 23:45:28 a following date command can be used: ~# date 032023452010.28
Sat Mar 20 23:45:28 EST 2010

Linux date command can be very flexible as it offers more ways on how to set a time on your Linux systems: the next example we set date and time to “05 JUL 2021 01:55:56”. ~# date -s "05 JUL 2021 01:55:56"
Mon Jul  5 01:55:56 EST 2021

In case that we want to set just time but date a following syntax to date command can be applied: ~# date +%T -s "18:30:01"
18:30:01 ~# date
Mon Jul  5 18:30:05 EST 2021

Date command is often used with connection to bash scripting. In the next example we create a simple bash script to create a backup of a user’s home directory and we will name the output backup file accordingly to a current date:$ cat
OF=myhome_directory_$(date +%Y%m%d).tar.gz
tar -czf $OF /home/linuxconfig$ chmod +x$ date
Wed May 12 12:47:39 EST 2010$ ./
tar: Removing leading `/' from member names$ ls  myhome_directory_20100512.tar.gz$

Linux date command is also capable to do some simple arithmetics as shown in the following example:$ echo "Today is `date +%A` `date +%e`th of `date +%B` \ 

`date +%G` which is `date +%j`th day since 1th of January `date +%G`"
Today is Wednesday 12th of May 2010 which is 132th day since 1th of January 2010

Source taken from