In the next article we are going to take a look at some concepts and options of the date command. When users use a Gnu / Linux distribution, we have a large handful of commands at our disposal. With them we will be able to obtain information about services, processes, system components and many other things. All these commands are focused on enabling proper administrative and support management.
One of these available commands is date. This is used for get current system date and time or even to set the system date. Although its most common use is print the date and time in different formats as well as calculate future and past dates. In the following lines we are going to see some basic concepts and options on the use of this command.
Use the date command in Ubuntu
To begin with, it must be said that the syntax for the date command is:
date [OPCIÓN] ... [+FORMATO]
If we want view current system time and date using the default format, in a terminal (Ctrl + Alt + T) you just have to type:
date
The output will include; the day of the week, the month, the day of the month, the time, the time zone, and the year.
Format options for the date command
The output of the date command we will be able to format with a sequence of characters to control the format preceded by a + sign. Format controls start with the% symbol and are replaced with their corresponding values in the output:
date +"Año: %Y, Mes: %m, Día: %d"
The character % Y will be replaced with the year, % m with the month and% d with the day of the month. Another example of format control would be the following:
date "+FECHA: %D%nHORA: %T"
Next we will see a small list of some of the most common formatting characters:
- % a → Abbreviated name of the day.
- % A → Full name of the day.
- % b → Abbreviated month.
- % B → Name of the full month.
- % d → Day of the month.
- % H → Time. From 00 to 23.
- % I → Time. From 01 to 12.
- % j → Numeric day of the year.
- % m → Month number.
- % M → Minutes.
- % S → Seconds.
- % u → Number of the day of the week.
- % Y → Full year.
For get a complete list of all formatting options, in the terminal we can use:
date --help
We can also use the corresponding man page:
man date
Date string
The -d option will allow us to use a specific date. We will be able to specify the date as a human-readable date string:
date -d "19:47:47 2019-02-09"
We can also use the custom format:
date -d '02 Feb 1982' +'%A, %d %B %Y'
The string can also accept values like; "tomorrow "," friday "," last friday "" next friday "," next month "," next week "..etc.
date -d "next month"
Another option available will be know the exact day of some date with the following line:
date -d "2019-06-28" +"%A"
Override current time zone
Default, the date command makes use of the time zone defined in the directory / etc / localtime. The environment variable TZ (TimeZone) can be used to override this behavior. To use a different time zone, the environment variable TZ can be set to the desired time zone.
For example, to display the time in Kabul, Asia, we would type in the terminal (Ctrl + Alt + T):
TZ='Asia/Kabul' date
To list all available time zones we can list the files in the / usr / share / zoneinfo directory or use the command timedatectl list-timezones.
How to convert epoch to a Linux date
The date command can be used as a converter. Epoch or Unix timestamps, is the number of seconds that have elapsed since January 1, 1970 at 00:00:00 UTC.
For print the number of seconds from that time to the current day, all you have to do is use the% s format control:
date +%s
We can also get the seconds until a certain date:
date -d "2019-06-05" +"%s"
For convert seconds to current date, you just have to set the seconds as a date string:
date -d @1559604647
Show the time of the last modification of a file with date
If we use the date command with the -r option we can obtain the time of the last modification of a file. For example: uterine
date -r /etc/hosts
If you want to modify the timestamp of the file, you can use the command touch.
Set the system date and time
If you want to set the system clock manually, you can use the option "–Set =”. For example, if we wanted to set the date and time to 2:30 pm on July 1, 2019, we would type:
date --set="20190701 14:30"
Generally, it is not recommended to set the system date and time manually with the date command. In most distributions, the system clock is synchronized by the services Ntp or systemd-timesyncd.
Nothing you put under Ubuntu Server has worked for me