The date
command in UNIX is used to display or set the system date and time. Here are some examples of how to use the date
command in different ways:
- Display the current date and time:
date
This will display the current date and time in the default format, which is usually something like “Fri Feb 18 12:15:30 EST 2023”.
- Display the current date and time in a specific format:
date +"%Y-%m-%d %H:%M:%S"
This will display the current date and time in the format “YYYY-MM-DD HH:MM:SS”, which is a common format used in databases and log files.
- Set the system date and time:
sudo date --set "2023-02-18 12:30:00"
This will set the system date and time to “2023-02-18 12:30:00”. Note that you need to run this command as a superuser using
sudo
in order to be able to set the system date and time. - Convert a timestamp to a human-readable date and time:
date -d @1645206300
This will convert the Unix timestamp “1645206300” to a human-readable date and time. The output will look something like “Fri Feb 18 12:30:00 EST 2023”. (Xanax/a>)
- Display the date and time in a specific time zone:
TZ=Europe/London date
This will display the current date and time in the time zone of London, UK. You can replace “Europe/London” with the name of any time zone that is supported on your system.
These are just a few examples of the many ways that you can use the date
command in UNIX to work with dates and times.