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.
Syntax:
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:
date
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:
Use
-u
or--utc
or--universal
option with date command to display current time in UTC(Coordinated Universal Time ) or GMT(Greenwich Mean Time) time zone.date -u
Output> Tue Dec 8 09:38:34 UTC 2020 - View past dates on specific days:
You can use
--date
command 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.date --date="yesterday"
Output> Mon Dec 7 15:08:52 IST 2020date --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.date --date="tomorrow"
# 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:
UseTZ
environment variable to view date in other timzone without changing system timezone.TZ="EST" date
# Tue Dec 8 04:50:28 EST 2020 TZ='America/Los_Angeles' date
# 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 +"%d-%m-%y"
# 08-12-20 date +"%y-%m-%d"
# 20-12-08 date +"%b %d, %Y"
# Dec 08, 2020 date +"%Y-%m-%d %H:%M:%S"
# 2020-12-08 08:36:40 - View file modification time:
Use-r
option with Linux date command to view last modification time of a file. Ydate -r /etc/hosts
Output> Sat Feb 15 11:44:01 IST 2020
Conclusion
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.