- How To Print Current Date and Time In Linux and Windows From Command Line?
- Linux/FreeBSD/Unix
- Only Hour and Minute
- Year Month Day Format
- Year-Month-Day Format
- Month/Day/Year Format
- Display Date and Time In Windows Command Line
- Python Datetime Module and Functions
- Display Date And Time In Linux
- Syntax
- Display date and time in Linux using the timedatectl
- TZ environment variable
- GUI Tool: Time Administration
- How to change date and time settings with Gnome based Linux desktop
- Conclusion
- HowTo: Get Current Time In Shell Script
- Example: Show current time
- Example: 12 hour clock time
- Sample shell script
- 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
How To Print Current Date and Time In Linux and Windows From Command Line?
Getting current date-time information is very different for different systems and programming languages. We will look at this simple but big issue for the vast majority of operating systems and programming languages.
Linux/FreeBSD/Unix
We can date information from the GUI panel clock but we will look for the command line. We will issue date command to get the current date and time. Linux date format has a lot of options. Alternatively, we can call these as Unix date or bash date examples.
Linux FreeBSD Unix date and time with Different Formats
Only Hour and Minute
We can provide parameters to get only time partition from date command.
Year Month Day Format
We can get current year month day like below
Year-Month-Day Format
We can use ready to use parameters like +%F which will list year-month-day
Month/Day/Year Format
We can use +%D to get a date according to locale settings
Display Date and Time In Windows Command Line
Windows operating system provides date and time commands in order to print and set current date and time. After issuing the command date and time the current date and time will be displayed and then we will be asked for a new date and time. We can skip this just by pressing Enter or simply do not entering any value.
Display Date and Time In Windows Command Line
Python Datetime Module and Functions
Python is a scripting language and to get current date time information python function in a code file can be used. An alternative is python interpreter. In this example, we will use an interpreter.
We can get time information with %I:%M format string parameters.
We get the year, month and day as a string like below
Python Datetime Module and Functions
Источник
Display Date And Time In Linux
H ow do I display date and time in Linux using the command line and GUI options?
To display date and time under Linux operating system using command prompt use the date command. It can also display the current time / date in the given FORMAT. We can set the system date and time as root user too.
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | No |
Requirements | Linux with the terminal application |
Est. reading time | 3 minutes |
Syntax
Open a terminal and type the following command:
date
You can format the date as follows in dd-mm-yy format:
date +»%d-%m-%y»
Simply display the current time:
date «+%T»
- 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 ➔
To print the date of the day before yesterday, run:
date —date=’2 days ago’
Want to see the day of year of Christmas in the current year? Try:
date —date=’25 Dec’ +%j
Display the current full month name and the day of the month:
date ‘+%B %d’
Display date and time in Linux using the timedatectl
Most modern Linux distro with systemd use the timedatectl command. It is used to query and change the system clock and its settings, and enable or disable time synchronization services. All you have to do is type the following command:
$ timedatectl
TZ environment variable
The TZ environment tells Linux what time zone you are in. Many times when you want to change your time zone temporarily. For example, you might want to print the current date and time in “America/Los_Angeles” timezone even though you are in “Europe/London”. So we can set TZ, give a command like as follows when using TCSH/CSH:
setenv TZ timezone
For BASH/KSH/SH (see export command:
TZ=timezone; export TZ
Another option:
TZ=»America/Los_Angeles» date
TZ=»Asia/Tokyo» date
Use the following command to print a list of all timezones:
timedatectl list-timezones
timedatectl list-timezones | more
## filter out data using the grep command/egrep command ##
timedatectl list-timezones | grep -i Hong_kong
timedatectl list-timezones | grep -i paris
timedatectl list-timezones | grep -E -i ‘paris|london|kolkata’
The TZ Environment Variable on Linux
GUI Tool: Time Administration
The Time Administration Tool allows you to set the time, date and timezone of your system, as well as setting any time server to synchronize your local time server. Type the following command to start time admin tool:
sudo time-admin
## OR ##
time-admin
Fig.01: Linux Date and Time Administration Tool
How to change date and time settings with Gnome based Linux desktop
First, you need to Settings in Activities and then click Details in the sidebar. Make sure you click Date & Time in the sidebar to open the panel:
Conclusion
In this quick tutorial, you learned about the date command that you can use to see or change the date/time under Linux operating systems. We further explained how to use the GUI tools too. The date command has many more options. See man page by typing the following man command:
$ man date
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник
HowTo: Get Current Time In Shell Script
H ow do I get the current server time in shell script on Linux or Unix-like operating systems? How do I store the current time in the shell variable and use in my scripts?
You can use the date command to display or set the current date and time. You need to use the date FORMAT syntax to controls the output of the date command. The %T format sequence interpreted by the date command to
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | No |
Requirements | None |
Est. reading time | Less than a minute |
display the current time. The syntax is:
date +%FORMAT
date +»%FORMAT»
var=$(date +»%FORMAT»)
Example: Show current time
Open a terminal and type the following command:
To store time to a shell variable called now, enter:
Example: 12 hour clock time
Pass the %r format to the date command:
To remove AM or PM from the output use, type:
- 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 ➔
Sample shell script
Run it as follows:
Animated gif 01: demo.sh script in action. Please note time is updating as screen refresh due to the clear command.
References:
- See date command man page for more information.
🐧 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.
ow=$(date +”%T”)
should be
now=$(date +”%T”)
Thanks for the heads up!
i need a shell script to implement backround process that will print current time (**not in command prompt)
if i want to run my shell script for only five minutes but that should be in loop.how can i do this??
Could you please give some examples ASAP?
put your shell script under crontab configure it as per your requirement
Hi,
I need shell script which need to check one folder every day and list the files and send a mail to mail box..
ex: I have below folder
#cd /image/abc/20140921/
#/image/abc/20140921/ ls -ltr *gif* | wc
but here last date folder name will change as each day date..so script has to check every days files and send mail..
Источник
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
Источник