FUNDAMENTALS A Complete Guide for Beginners
The date command is one of the most frequently used commands in Linux. It is used to get or set the date and time of the system. By default, the date command displays the Central Standard Time (CST) Time Zone. But you can adjust the date view on your terminal as well. In this article, I will show you the uses of the date command in Linux with some hands-on examples.
A. Description
The basic use of the date command in Linux is to show the system’s current date and time. This command can be used to find out the modification time of files and can calculate the past and future dates as well. You can also change the machine’s date and time using the date command. But you will need to write the command as the superuser. The command also provides you with options and formats to improve your experience.
B. Syntax
The syntax for the date command in Linux is really simple. You can type the command alone or provide some option or format according to your need.
date [OPTION]... [+FORMAT]
C. Options
Many useful options are available for the date command. I have listed some of the most used ones here. However, you can always look at the man page to learn more.
man date
Useful Options
- -d/–date=STRING ( Converts time from the string [today, tomorrow, 1 year ago, etc] )
- -f/–file=DATEFILE ( Runs date command for each line of the file )
- -s/–set=STRING ( Sets time described by the string)
- -r ( Returns last modification time of a file )
- TZ ( Prints out time in a given time zone )
- -u ( Returns the time in UTC time zone )
D. Formats
Format controls the output shown in the Terminal using the date command in Linux. You can modify how you want to view the specified date and time. There are a busload of formats available for the date command. I have listed the useful ones here. You will find the full list on the man page.
man date
Useful Formats
- %a ( Displays the abbreviated day names [Sun to Sat] )
- %A ( Displays the full-day names [Sunday to saturday] )
- %b ( Displays the abbreviated month names [Jan to Dec] )
- %B ( Displays the full month names [January to December] )
- %d ( Display the day of the month [01 to 31] )
- %D ( Display date as mm/dd/yy )
- %H ( Display the hour )
- %m ( Displays the month of the year [01 to 12] )
- %M ( Displays the minute )
- %S ( Display the seconds )
- %T ( Display the time in 24-hour as HH:MM:SS )
- %y ( Displays the last two digits of the year [00 to 99] )
- %Y ( Displays year in four-digit )
Practical Examples of the “date” Command in Linux
The date command in Linux, allows you to view and alter system date and time. A few other applications of the command are also available Here are some practical examples of the date command to enhance your experience in Linux.
Example 1: Display the Current Date and Time with the Abbreviated Day of the Week and Time Zone Using the “date” Command in Linux
Using the date command in Linux, you can see the current date and time set on your machine with the abbreviated day of the week and time zone. In this example, I will show you the date and time of my system. You can also do the same by following the steps below.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
date
➌ Now, press the ENTER button.
Output >
In the image below, you can see that I have printed out the current date with the abbreviated day of the week and time zone using the date command.
Similar Readings
- The “man” Command in Linux [6 Practical Examples]
- The “history” Command in Linux [6 Practical Examples]
- The “cal” Command in Linux [7 Practical Examples]
- The “bc” Command in Linux [10+ Practical Examples]
Example 2: Display the Current Date and Time in UTC Time Zone Using the “date” Command in Linux
Using the date command in Linux with the -u option, you can change the current time zone to UTC time zone while viewing the current date and time. In this example, I will show you the date and time of my system in the UTC time zone. To do so you can follow the given process.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
date -u
➌ Now, press the ENTER button.
Output >
In the image below, you can see that I have printed out the current date and time in the UTC time zone using the date command.
Example 3: Converting a Given String into a Date and Time Using the “date” Command in Linux
You can pass a string to the date command in Linux and convert it to the corresponding date and time. To do so, you will need to use the option -d/–date. In this example, I will illustrate the conversion of a given string with multiple cases. These cases include strings representing current, past, and future dates. You can follow the steps below to apply the same conversions.
Example A: Getting Current Date from a String Using the “date” Command in Linux
To get the current date from a string you can type the following command.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following commands in the command prompt:
date --date="today"
OR,
date -d today
➌ Now, press the ENTER button.
Output >
In the image below, you can see that I have printed the current date from the string “today” using the date command in Linux.
Example B: Getting a Past Date from a String Using the “date” Command in Linux
To get a date from the past using a string you can type the following command.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following commands in the command prompt:
date --date="2 years ago"
OR,
date -d "2 years ago"
➌ Now, press the ENTER button.
Output >
In the image below, you can see that I have printed out a past date from the string “2 years ago” using the date command in Linux.
Example C: Getting a Future Date from a String Using the “date” Command in Linux
To get a date from the future using a string you can type the following command.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following commands in the command prompt:
date --date="tomorrow"
OR,
date -d tomorrow
➌ Now, press the ENTER button.
Output >
In the image below, you can see that I have printed out a date from the future using the string “tomorrow”.
Example 4: Setting the System Date and Time Using the “date” Command in Linux
Using the date command in Linux with the –set option, you can set the system date and time. To do so you must act as the superuser using the sudo command. In this example, I will change the date and time of my system. You can also do the same by following the steps below.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
sudo date --set="Nov 13 03:23:34 PM CST 2016"
➌ Type user Password
❹ Now, press the ENTER button.
Output >
In the image below, you can see that I have changed the system’s date and time to November 13th 03:23:24PM.
Example 5: Viewing Specific Time Zone Using the “date” Command in Linux
You will be able to view date and time from different time zones using the date command along with the TZ variable. In this example, I will view the time of Dhaka, Bangladesh using the date command. To do so you can follow the steps below.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
TZ='Bangladesh/Dhaka' date
➌ Now, press the ENTER button.
Output >
In the image below, you can see that I have displayed the date and time of Dhaka, Bangladesh.
Example 6: Getting the Last Modification Time of a File Using the “date” Command in Linux
Using the date command in Linux with the -r option, you can see the last modification time of a file. In this example, I find out the last modification time of the file “employee.txt”. You can also do the same by following the steps below.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
date -r employee.txt
➌ Now, press the ENTER button.
Output >
In the image below, you can see that I have printed out the recent modification time of the desired file.
Similar Readings
- The “cron” Command in Linux [3 Practical Examples]
- The “crontab” Command in Linux [10 Practical Examples]
- The “ncal” Command in Linux [8 Practical Examples]
Example 7: Extracting Date and Time from a Date File Using the “date” Command in Linux
You can display multiple dates from a datefile using the date command in Linux. To do so, you will need to use the option -f/–file. In this example, I will extract several dates from the file “Dates.txt”. You can follow the steps below to do the same.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
date --file=Dates.txt
➌ Now, press the ENTER button.
Output >
In the image below, you can see that at first, I showed the Dates.txt file contents using the cat command. Then I displayed all the dates in proper format using the date command.
Example 8: Specifying Date Formats Using the “date” Command in Linux
You can view the date and time from your command line in a specified pattern using the available formats. Below I am presenting you some date formats and their corresponding outputs. You can combine several formats according to your need.
The “date” Command With Format | Output |
---|---|
date “+%D” | 01/02/23 |
date “+%D %T” | 01/02/23 03:08:14 |
date “+%d-%m-%Y” | 02-01-2023 |
date “+%A” | Monday |
date “+%A %B %d %T %y” | Monday January 02 03:11:11 23 |
You can apply a format by following the given steps.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
date "+%A %B %d %T %y"
➌ Now, press the ENTER button.
Output >
In the image below, you can see that at first, I printed out the current date in my desired format using the date command.
Conclusion
In this article, I have shown you some of the basic applications of the date command in Linux. You can get a more diverse experience by combining multiple options and formats together. I hope these practical examples of the date command will help you engage with Linux and ease your learning of the Command line.
Similar Readings
- The “neofetch” Command in Linux [12+ Practical Examples]
- The “tree” Command in Linux [14 Practical Examples]
- The “whatis” Command in Linux [12 Practical Examples]
- The “whereis” Command in Linux [10 Practical Examples]
- The “which” Command in Linux [3 Practical Examples]
- The “at” Command in Linux [7 Practical Examples]
- The “tee” Command in Linux [4 Practical Examples]