date is an useful Linux command to view or change current date on system. All the Linux operating systems are synced with the NTP (Network Time Protocol) servers. So its rarely required to configure system time manually. You only need to change timezone as per your requirements.
This tutorial will described you to uses of date command on Linux system.
Usage: date [OPTION]... [+FORMAT] or: date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
View Current Date
The default date command displays the current date and time on standard output. This date/time will be in default system time zone.
Open a terminal (CTRL + ALT + T) and type:
Output>Tue Dec 8 15:08:21 IST 2020
The above command shows current time in EST timezone, which is the default timezone of my Linux system.
date Command Examples
- Show date time in UTC/GMT:
--universaloption with date command to display current time in UTC(Coordinated Universal Time ) or GMT(Greenwich Mean Time) time zone.
Output>Tue Dec 8 09:38:34 UTC 2020
- View past dates on specific days:
You can use
--datecommand line option to view date in past. Like what was the date 7 days ago, 20 days ago, 2 month ago, last Friday, yesterday and so on.
Output>Mon Dec 7 15:08:52 IST 2020
date --date="7 days ago"
# Tue Dec 1 15:09:23 IST 2020
date --date="20 days ago"
# Wed Nov 18 15:09:42 IST 2020
date --date="5 months ago"
# Wed Jul 8 15:09:55 IST 2020
date --date="last friday"
# Fri Dec 4 00:00:00 IST 2020
- View future dates:
Similarly, you can view dates in future. For example, what will be date on tomorrow, next friday, after 20 days, after 3 months and so on.
# Wed Dec 9 15:10:50 IST 2020
date --date="next friday"
# Fri Dec 11 00:00:00 IST 2020
date --date="+20 days"
# Mon Dec 28 15:20:08 IST 2020
date --date="+3 months"
# Mon Mar 8 15:20:18 IST 2021
You can use this feature to detect month end. For example, If tomorrow is 01, then today is the last date of monthly. Which can be useful for running cronjobs on end of the months.
[ `date --date="tomorrow" +"%d"` -eq 09 ] && echo "Today is last day of month"
- View date in other timezone:
TZenvironment variable to view date in other timzone without changing system timezone.
# Tue Dec 8 04:50:28 EST 2020
# Tue Dec 8 01:50:55 PST 2020
- Print date in specific format:
The default date command print complete date and time including day and timezone. You can also customize it to print specific date format.
date +"%b %d, %Y"
# Dec 08, 2020
date +"%Y-%m-%d %H:%M:%S"
# 2020-12-08 08:36:40
- View file modification time:
-roption with Linux date command to view last modification time of a file. Y
date -r /etc/hosts
Output>Sat Feb 15 11:44:01 IST 2020
In this tutorial, you have learned about date command in Linux with useful examples. Hope you understand the uses of date command in Linux system.
If you like this post, please od share this with your social network.