Introduction
Linux date
command displays and sets the system date and time. This command also allows users to print the time in different formats and calculate future and past dates.
Read on to learn how to use the date
command in Linux.
Prerequisites
- A system running Linux
- A user account with root privileges
- Access to a terminal window/command line
Linux date Command Syntax
The syntax for the date
command is:
date [option]... [+format]
How to Use date Command in Linux
To show the current system time and date, type in the date
command:
date
The output displays the day of the week, day of the month, month, year, current time, and time zone. By default, the date
command is set to the time zone of the operating system.
The -d
option allows users to operate on a specific date. For example, we can type in the following command:
date -d "2000-11-22 09:10:15"
You can use the --date
command to display the given date string in the format of a date. This command does not affect the system’s actual date and time values, and it only prints the requested date. For example:
date --date="09/10/1960"
Linux date Command Format Options
To format the date
command’s output, you can use control characters preceded by a +
sign. Format controls begin with the % symbol and are substituted by their current values.
Here, the %Y character is replaced with the current year, %m with month, and %d with the day of the month:
date +"Year: %Y, Month: %m, Day: %d"
Here are another two formatting examples:
date "+DATE: %D%nTIME: %T"
date +"Week number: %V Year: %y"
These are the most common formatting characters for the date
command:
-
%D
– Display date as mm/dd/yy%Y
– Year (e.g., 2020)%m
– Month (01-12)%B
– Long month name (e.g., November)%b
– Short month name (e.g., Nov)%d
– Day of month (e.g., 01)%j
– Day of year (001-366)%u
– Day of week (1-7)%A
– Full weekday name (e.g., Friday)%a
– Short weekday name (e.g., Fri)%H
– Hour (00-23)%I
– Hour (01-12)%M
– Minute (00-59)%S
– Second (00-60)
To see all formatting options, run date --help
or the man command man date
in your terminal.
Set or Change Date in Linux
To change the system clock manually, use the set command. For example, to set the date and time to 5:30 PM, May 13, 2010, type:
date --set="20100513 05:30"
Most Linux distributions have the system clock synchronized using the ntp
or the systemd-timesyncd
services, so be careful when the setting the clock manually.
Display Past Dates
Use the --date
option to display past dates in Linux. The date
command accepts values such as "tomorrow"
, "Friday"
, "last Friday"
, "next Friday"
, "next week"
, and similar. So, use the following strings to print past dates::
date --date="2 year ago"
date --date="yesterday"
date --date="10 sec ago"
Display Future Dates
The --date
option can also display future dates. Like with past dates, you can type in strings to print upcoming dates:
date --date="next monday"
date --date="4 day"
date --date="tomorrow"
Display the Date String at Line of File
The --file
option prints the date string present at each line of the file. Unlike the --date
option, --file
can present multiple date strings at each line.
This is the syntax for the --file
command:
date --file=file_name.txt
Here we use the cat command to add dates to a file and then print them with the date command:
Display Last Modified Timestamp of a Date File
When you use the -r
option, the date
command prints the last modification time of a file. For example, the following command prints the last time the hosts file was changed:
date -r /etc/hosts
Override a Time Zone
By default, the date
command uses the time zone defined in /etc/localtime
. To use a different time zone in the environment, set the TZ
variable to the desired time zone.
For example, to switch to New York time, enter:
TZ='America/New_York' date
Type in the date
command to return the system to its default time zone. To see all available time zones, use the timedatectl list-timezones
command.
The date
command can also show the local time for a different time zone. For example, to display the local time for 4:30 PM next Monday on the Australian east coast, type:
date -d 'TZ="Australia/Sydney" 04:30 next Monday'
Use date with Other Commands
You can use the date
command to create file names that contain the current time and date. The input below creates a backup MySQL file in the format of the current date:
mysqldump database_name > database_name-$(date +%Y%m%d).sql
Another common use of the date
command is in shell scripts. Below we assign the output of date
to the date_now
variable:
date_now=$(date "+%F-%H-%M-%S")
Use Unix Epoch Time (Epoch Converter)
You can use the date
command as an Epoch converter. Epoch, or Unix timestamps, is the number of seconds that have passed since January 1, 1970, at 00:00:00 UTC.
To show the number of seconds from the epoch to the current day, use the %s
format control:
date +%s
To see how many seconds passed from epoch to a specific date, enter:
$ date -d "1984-04-08" +"%s"
Conclusion
You now have a good understanding of how to use the date
command in Linux. If you are interested in more date/time configuration options for Linux, read How to Set or Change Timezone/Date/Time on Ubuntu.
原创文章,作者:506227337,如若转载,请注明出处:https://blog.ytso.com/223850.html