+3 votes
288 views
How to use the date date command in Linux

in Linux / Unix by (551k points)
reopened | 288 views

1 Answer

+4 votes
Best answer

1. Basic use date command in Linux
2. Format options in date Linux
3. Edit the current time zone with date Linux command
4. Use of seconds with date Linux command
5. How to convert epoch to a Linux date

When using Linux distributions we have at our disposal hundreds of commands that help us obtain accurate information about services, processes , system components and much more, all of this focused on proper administrative and support management..

One of these commands is the date which is used to obtain the current date and time of the selected system:

image

But more than this, this command gives us the possibility to see or calculate a date in a format that we specify or the root user can use it to configure the operating system clock if necessary.

TechnoWikis will make a study on this command and its forms of use in Linux..


1. Basic use date command in Linux


We can see in the image above that when only the date command is executed without any additional parameters, it will display the current date and time of the system, including the day of the week, month, time, time zone and year.

If we wish to display the results on a specific date, it will be necessary to use the parameter -de entering the respective date:

 date -d "2018-01-15" 
image

This date command has multiple options at the display format level. For example, it will be possible to indicate the date with the format string by preceding it with a plus sign as follows:

 date + "Week number:% V Year:% y" 
This will result in the current week number of the current year since% V is the format option to display the current week number, and% and represents the last two digits of the year. image

2. Format options in date Linux


Some of the most common formatting options when using date are:
Refers to the abbreviated name of the week
 %to 

Indicate the full name of the week
 %TO 

Abbreviated Name of the Month
 % b 

Full name of the month
 % B 

Display local date and time
 %C 

Display the day of the month
 % d 

It shows us the day of the current year
 % j 

Reflect the full date
 %F 

Indicate the elapsed minutes
 % M 

It allows us to see the nanoseconds elapsed
 % N 

Display time in 24-hour format
 % R 

Show the number of the week taking Monday as the first day of the week
 % W 

Show the number of the week taking Sunday as the first day of the week
 %OR 

To see the various options we can execute the following command:

 date –help 
image

3. Edit the current time zone with date Linux command


By default, the date command makes use of the time zone defined in the / etc / localtime directory. The environment variable TZ (TimeZone) can be used to cancel this behavior, we can check the TZ using the following command:
 TZ = GMT date 
image

All valid time zones can be found in the / usr / share / zoneinfo / directory:

image

Some examples of how we can use the date command defining certain times are:

Now
 date -d now 

Today
 date -d today 

Yesterday
 date -d yesterday 

morning
 date -d tomorrow 

Next Sunday
 date -d sunday 

Last sunday
 date -d last-sunday 
image
Other options we can use
  • last week
  • next week
  • last month
  • next-month
  • last year
  • next-year

4. Use of seconds with date Linux command


Using the date command allows us to perform another type of analysis, for example, we can take the date command to convert a specific date or time to the time of the Unix era (seconds from 00:00:00, January 1, 1970 ) and vice versa, in this case, we will execute the following with which we will see the epoch seconds at the current time:
 date +% s 
The result will be as follows: image

Using this same format we can obtain the seconds of a certain date, for example:

 date -d "2017-12-31" + "% s" 
image

5. How to convert epoch to a Linux date


Recall that epoch is the UNIX time format.
With date it is possible to convert epoch to a specific date using one of the following commands:
 date -d "UTC 2000-01-15 123456897 secs" date -d @ 123456897 
image

We also have the possibility of knowing the exact day of some date with the following line:

 date -d "2000-01-15" + "% A" 
image

Set a date manually and use the date command in Scripts and commands When it is necessary to set a date or time different from the current one, just run the following syntax:

 date --set = "YYYYMMDD HH: SS" 
It is possible to assign the result of the date command to a shell variable and then use it later in various scripts, for example, we can execute the following:
 STARTTIME = `date` echo $ STARTTIME sleep 7 echo $ STARTTIME 

image

We can see how the date command allows us to perform actions far beyond simply seeing the current date and time.


by (3.5m points)
edited

Related questions

+3 votes
1 answer
asked Sep 17, 2019 in Linux / Unix by backtothefuture (551k points) | 268 views
+3 votes
1 answer
+4 votes
1 answer
asked Oct 4, 2019 in Linux / Unix by backtothefuture (551k points) | 380 views
+3 votes
1 answer
asked Oct 20, 2020 in Linux / Unix by backtothefuture (551k points) | 220 views
+4 votes
1 answer
asked Sep 22, 2020 in Linux / Unix by backtothefuture (551k points) | 311 views
Sponsored articles cost $40 per post. You can contact us via Feedback
10,634 questions
10,766 answers
510 comments
3 users