How to Calculate the Date in Unix
The Unix date command has been designed to display the system date and time, read directly from the operating system's kernel clock. The output of the date command can be displayed according to a wide variety of user defined formats. Using the Unix shell, it is also possible to calculate addition and subtraction of time to or from the date command's output. Typically, you would perform this type of work in a shell script, but it is possible to execute simple calculations with the date directly from the Unix command line.
Instructions
-
-
1
Open a terminal window. Open an xterm, kterm or your preferred method of accessing the command line.
-
2
Type the command "date" on the command line. The date command will display the current date on your system in a standard format including the day of the week, month, day, hour, minute, second, timezone and year.
-
-
3
Adjust the output of the date command using the "+" flag with appropriate operands. For example, the command "date +%Y" will display only the current year. Consult the man page for the date command for all specific output options available.
-
4
Execute the bash shell with the command "bash" on the command line. This will simply ensure the ensuing command returns a proper result, so if you already use the bash shell you can skip this step.
-
5
Evaluate the date command with the eval shell command to perform calculations. The eval command can either be used in a shell script or from the command line. For example, the command "eval echo $(date +'$((%Y+5))')" will add five years to the current date and return the result.
-
1
Tips & Warnings
To access the Unix manual page for the date command, type "man date" from the command prompt in your terminal session.
The Unix date command can also be used to reset your computer's clock, so be careful which flags you use with the command.