- 12.3. Команды для работы с датой и временем
- Linux: Bash Get Time
- Syntax
- A list of date command field descriptors
- How To Get / Print Current Date in Unix / Linux Shell Script
- Print current date and time in Unix shell script
- Print Current Date in Unix
- Getting the current date and time in Linux shell script
- A list of date command format codes
- Sample shell script to display the current date and time
- Conclusion
12.3. Команды для работы с датой и временем
Время/дата и измерение интервалов времени
Команда date без параметров выводит дату и время на стандартное устройство вывода stdout. Она становится гораздо интереснее при использовании дополнительных ключей форматирования вывода.
Пример 12-7. Команда date
Ключ -u дает UTC время (Universal Coordinated Time — время по Гринвичу).
Отображает время для указанной временной зоны.
Выводит подробную статистику по исполнению некоторой команды.
time ls -l / даст нечто подобное:
См. так же очень похожую команду times, обсуждавшуюся в предыдущем разделе.
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | No |
Requirements | None |
Est. reading time | 1m |
[/donotprint]
Syntax
To display current time, enter:
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
You can only display time, enter:
$ date +»%T»
Sample outputs:
The following command use your local standards to display date and time:
$ date +»%c»
Sample outputs:
To display the date and time in a specified format, enter:
date +»%r %a %d %h %y (Julian Date: %j)»
Sample outputs:
A list of date command field descriptors
Finally, you can use TZ variable as follows. For example, show the time on the west coast of the US:
$ TZ=’America/Los_Angeles’ date
Sat Sep 19 12:26:57 PDT 2015
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Category | List of Unix and Linux commands |
---|---|
Documentation | help • mandb • man • pinfo |
Disk space analyzers | df • duf • ncdu • pydf |
File Management | cat • cp • less • mkdir • more • tree |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Linux Desktop Apps | Skype • Spotify • VLC 3 |
Modern utilities | bat • exa |
Network Utilities | NetHogs • dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop |
Searching | ag • grep • whereis • which |
Shell builtins | compgen • echo • printf |
Text processing | cut • rev |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Comments on this entry are closed.
And to set the time trought BASH, use this:
where
MM = Month (2 digits)
DD = Day (2 digits)
HH= Hours (24 hours format)
mm= Minutes (2 digits)
YYYY= Year (4 digits)
Example:
date 102712232010
Set the time to Wed Oct 27 12:23:00 CDT 2010
It’s not
$ date +”%T”
@soubhik — Your comment is inaccurate. Read more on the man page for /bin/sh or any shell.
Those commands that you posted, in this case, are equivalent. Read the section on quoting.
Setting a multi time zone clock on a corner of my desktop. Using NerdTool shell scripting. I have tried making use of the different time zone example on this page. Problem is Unless it is immediate close to local I get a default of several hours – not at all what I expected to get. Can anyone please tell me what I am doing wrong? Thanks.
TZ=’America/Hawaii’ date “+%H:%M %p”
TZ=’America/Los_Angeles’ date “+%H:%M %p”
TZ=’America/Denver’ date “+%H:%M %p”
TZ=’America/Dallas’ date “+%H:%M %p”
TZ=’America/Miami’ date “+%H:%M %p”
PS – I am not an expert in this, just trying examples out and doing my best to modify as the index reveals options. The example uses the dollar sign to precede the code but doing that kills the script in NerdTool.
Источник
How To Get / Print Current Date in Unix / Linux Shell Script
H ow do I get the current date in Unix or Linux shell scripting and store it into a shell variable? How do I print the current date using Unix shell script? How can I display the current time in Linux shell script?
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | No |
Requirements | Linux or Unix |
Est. reading time | 3 minutes |
You need to use the following syntax to print current date and time on screen:
Print current date and time in Unix shell script
To store current date and time to a variable, enter:
now=$(date)
OR
now=`date`
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
Print Current Date in Unix
To print this date either use the printf or echo statement:
echo «$now»
echo «Current date: $now»
OR use the printf command:
printf «%s\n» «$now»
OR
printf «Current date and time in Linux %s\n» «$now»
Getting the current date and time in Linux shell script
You can format and display date using the following syntax:
Finding the current date and time in Linux or Unix using the date command
A list of date command format codes
FORMAT code | Description |
---|---|
%% | a literal % |
%a | locale’s abbreviated weekday name (e.g., Sun) |
%A | locale’s full weekday name (e.g., Sunday) |
%b | locale’s abbreviated month name (e.g., Jan) |
%B | locale’s full month name (e.g., January) |
%c | locale’s date and time (e.g., Thu Mar 3 23:05:25 2005) |
%C | century; like %Y, except omit last two digits (e.g., 20) |
%d | day of month (e.g., 01) |
%D | date; same as %m/%d/%y |
%e | day of month, space padded; same as %_d |
%F | full date; same as %Y-%m-%d |
%g | last two digits of year of ISO week number (see %G) |
%G | year of ISO week number (see %V); normally useful only with %V |
%h | same as %b |
%H | hour (00..23) |
%I | hour (01..12) |
%j | day of year (001..366) |
%k | hour, space padded ( 0..23); same as %_H |
%l | hour, space padded ( 1..12); same as %_I |
%m | month (01..12) |
%M | minute (00..59) |
%n | a newline |
%N | nanoseconds (000000000..999999999) |
%p | locale’s equivalent of either AM or PM; blank if not known |
%P | like %p, but lower case |
%q | quarter of year (1..4) |
%r | locale’s 12-hour clock time (e.g., 11:11:04 PM) |
%R | 24-hour hour and minute; same as %H:%M |
%s | seconds since 1970-01-01 00:00:00 UTC |
%S | second (00..60) |
%t | a tab |
%T | time; same as %H:%M:%S |
%u | day of week (1..7); 1 is Monday |
%U | week number of year, with Sunday as first day of week (00..53) |
%V | ISO week number, with Monday as first day of week (01..53) |
%w | day of week (0..6); 0 is Sunday |
%W | week number of year, with Monday as first day of week (00..53) |
%x | locale’s date representation (e.g., 12/31/99) |
%X | locale’s time representation (e.g., 23:13:48) |
%y | last two digits of year (00..99) |
%Y | year |
%z | +hhmm numeric time zone (e.g., -0400) |
%:z | +hh:mm numeric time zone (e.g., -04:00) |
%::z | +hh:mm:ss numeric time zone (e.g., -04:00:00) |
%. z | numeric time zone with : to necessary precision (e.g., -04, +05:30) |
%Z | alphabetic time zone abbreviation (e.g., EDT) |
Sample shell script to display the current date and time
Conclusion
You learned how to display the current date and time on Linux and Unix-like systems. We also explained how to store date or time in a shell variable. For more info see date command man page by typing the following date command or GNU/date help page here:
man date
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник