Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to use date command in day to day practical usage
In this article, we will learn about the date command in Linux and how to practically use it in day-to-day usage with some practical examples. The date command is used to print or change the system date and time, making it an essential tool for system administrators and users.
General Syntax
[root@localhost ~]# date [OPTION]... [+FORMAT] [root@localhost ~]# date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
Key Features of the Date Command
Print current date and time on the system
Display date and time in custom formats
Read dates from files
Display Universal Coordinated Time (UTC)
Set system date and time
Calculate past and future dates
Basic Usage Examples
Displaying Current Date and Time
[root@localhost ~]# date Wed Jun 15 04:10:29 IST 2016
Parsing Date from String
The --date option allows you to parse and display dates from various string formats −
[root@localhost ~]# date --date="6/15/2016" Wed Jun 15 00:00:00 IST 2016 [root@localhost ~]# date --date="next Monday" Mon Jun 20 04:10:29 IST 2016
Reading Dates from Files
Use the --file option to read dates from a file. Create a sample file first −
[root@localhost ~]# vi samplelog.txt # File contents: 04 jun 1978 03 aug 1980 24 feb 2004 22 dec 2007
[root@localhost ~]# date --file=samplelog.txt
Sun Jun 4 00:00:00 IST 1978 Thu Aug 3 00:00:00 IST 1980 Tue Feb 24 00:00:00 IST 2004 Sat Dec 22 00:00:00 IST 2007
Working with Past and Future Dates
The date command excels at calculating relative dates using natural language expressions −
[root@localhost ~]# date --date='1 day ago' Tue Jun 14 16:01:31 IST 2016 [root@localhost ~]# date --date='yesterday' Tue Jun 14 16:03:01 IST 2016 [root@localhost ~]# date --date='1 month ago' Sun May 15 16:03:35 IST 2016 [root@localhost ~]# date --date='1 year ago' Mon Jun 15 16:03:57 IST 2015 [root@localhost ~]# date --date='next Friday' Fri Jun 17 16:04:12 IST 2016
Setting System Date and Time
Use the -s option to set the system date and time (requires root privileges) −
# date -s "Tue June 15 04:08:37 IST 2016" Wed Jun 15 04:08:37 IST 2016
Working with Time Zones
Displaying UTC Time
[root@localhost ~]# date Wed Jun 15 04:10:29 IST 2016 [root@localhost ~]# date -u Tue Jun 14 22:40:31 UTC 2016
Converting Time Zones
[root@localhost ~]# date -d '2016-05-20 18:00 CST' Sat May 21 05:30:00 IST 2016
File Timestamps
Check when a file was last modified using the -r option −
[root@localhost ~]# date -r samplelog.txt Wed Jun 15 15:45:37 IST 2016
Custom Date Formatting
The date command supports extensive formatting options. Here are the most commonly used format specifiers −
| Format | Description | Example |
|---|---|---|
| %Y | 4-digit year | 2016 |
| %y | 2-digit year | 16 |
| %m | Month (01-12) | 06 |
| %B | Full month name | June |
| %b | Abbreviated month | Jun |
| %d | Day of month | 15 |
| %A | Full weekday name | Wednesday |
| %a | Abbreviated weekday | Wed |
| %H | Hour (00-23) | 14 |
| %I | Hour (01-12) | 02 |
| %M | Minutes | 30 |
| %S | Seconds | 45 |
Practical Format Examples
[root@localhost ~]# date +"%Y-%m-%d" 2016-06-15 [root@localhost ~]# date +"%B %d, %Y" June 15, 2016 [root@localhost ~]# date +"%A, %b %d at %I:%M %p" Wednesday, Jun 15 at 04:10 PM [root@localhost ~]# date +"%s" 1466011829
Common Use Cases
Log file naming:
logfile_$(date +%Y%m%d).logBackup scripts:
backup_$(date +%Y%m%d_%H%M%S).tar.gzScheduling validation: Check if current time meets specific criteria
Time zone conversions: Convert timestamps between different zones
Date arithmetic: Calculate deadlines, intervals, and relative dates
Conclusion
The date command is a powerful and versatile tool for handling date and time operations in Linux. From basic display functions to complex date arithmetic and formatting, it serves as an essential utility for system administration, scripting, and daily command-line tasks. Its ability to parse natural language date expressions and work with multiple time zones makes it indispensable for modern system management.
