Tutorial details | |
---|---|
Difficulty | Easy (rss) |
Root privileges | No |
Requirements | Python v2.x+ |
Time | 2m |
How to get current date and time in Python?
The syntax is:
time.strftime(format) ## 24 hour format ## time.strftime("%H:%M:%S") ## 12 hour format ## time.strftime("%I:%M:%S")
Here is another example done using datetime module:
#!/usr/bin/python3 from datetime import date today = date.today() fdate = date.today().strftime('%d/%m/%Y') print("Today's current date is -", today) print("Date in dd/mm/YYYY format -", fdate)
Getting current time in python using time module
Let us see some examples of time module:
Examples
A simple program in python to get today’s date and time:
#!/usr/bin/python import time print (time.strftime("%H:%M:%S")) ## 12 hour format ## print (time.strftime("%I:%M:%S"))
Sample outputs:
23:46:08 11:46:08
To print current date use:
#!/usr/bin/python import time ## dd/mm/yyyy format print (time.strftime("%d/%m/%Y"))
Sample outputs:
04/10/2013
Getting locals date and time in Python
#!/usr/bin/python # Purpose: Python Current Date Time Demo # Author: nixCraft # -------------------------------------------- import time now = time.strftime("%c") ## date and time representation print "Current date & time " + time.strftime("%c") ## Only date representation print "Current date " + time.strftime("%x") ## Only time representation print "Current time " + time.strftime("%X") ## Display current date and time from now variable print ("Current time %s" % now )
Sample outputs:
Current date & time Sat Oct 5 00:04:59 2013 Current date 10/05/13 Current time 00:04:59 Current time Sat Oct 5 00:04:59 2013
Format strings to get the current date and time in Python
The following directives can be embedded in the format string:
Directive | Meaning |
---|---|
%a | Weekday name. |
%A | Full weekday name. |
%b | Abbreviated month name. |
%B | Full month name. |
%c | Appropriate date and time representation. |
%d | Day of the month as a decimal number [01,31]. |
%H | Hour (24-hour clock) as a decimal number [00,23]. |
%I | Hour (12-hour clock) as a decimal number [01,12]. |
%j | Day of the year as a decimal number [001,366]. |
%m | Month as a decimal number [01,12]. |
%M | Minute as a decimal number [00,59]. |
%p | Equivalent of either AM or PM. |
%S | Second as a decimal number [00,61]. |
%U | Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0. |
%w | Weekday as a decimal number [0(Sunday),6]. |
%W | Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0. |
%x | Appropriate date representation. |
%X | Apropriate time representation. |
%y | Year without century as a decimal number [00,99]. |
%Y | Year with century as a decimal number. |
%Z | Time zone name (no characters if no time zone exists). |
%% | A literal ‘%’ character. |
Get the current date and time in Python using datetime module
The syntax is:
now = datetime.datetime.now() now.hour now.mintue now.year now.day now.month
Examples
#!/usr/bin/python import datetime i = datetime.datetime.now() print ("Current date & time = %s" % i) print ("Date and time in ISO format = %s" % i.isoformat() ) print ("Current year = %s" %i.year) print ("Current month = %s" %i.month) print ("Current date (day) = %s" %i.day) print ("dd/mm/yyyy format = %s/%s/%s" % (i.day, i.month, i.year) ) print ("Current hour = %s" %i.hour) print ("Current minute = %s" %i.minute) print ("Current second = %s" %i.second) print ("hh:mm:ss format = %s:%s:%s" % (i.hour, i.month, i.second) )
Sample outputs:
Current date & time = 2013-10-05 00:15:31.769049 Date and time in ISO format = 2013-10-05T00:15:31.769049 Current year = 2013 Current month = 10 Current date (day) = 5 dd/mm/yyyy format = 5/10/2013 Current hour = 0 Current minute = 15 Current second = 31 hh:mm:ss format = 0:10:31
I tested above programes with both Python 2 and Python 3:
How to get current date and time in Python?
#!/usr/bin/python from datetime import datetime i = datetime.now() print str(i) print i.strftime('%Y/%m/%d %H:%M:%S')
Sample outputs:
2013-10-05 00:20:30.495228 2013/10/05 00:20:30
Conclusion
You learned how to to get today’s date and current date and time in Python using various methods. See python time and datetime module for more info.
🐧 Please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
- RSS feed or Weekly email newsletter
- Share on Twitter • Facebook • 16 comments... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | 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 • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
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 |
Hey Nixcraft, that’s an excellent tutorial on obtaining the current date and time. Thanks a lot for sharing
Thanks for this helpful post!
What a phenomenal tutorial! I stumbled upon it by accident while doing homework, solved a lot of problems I have had in the past! Thanks a ton!
very helpful post… i wondered on the internet but this is exactly what i was looking for!!!!
great work . thanks a lot
Hi,
Thanks, it is so helpful, many thanks again.
Thanks for the tutorial!
Just one typo:
print (“hh:mm:ss format = %s:%s:%s” % (i.hour, i.month, i.second) )
I have a feeling that the second entry in the tuple should be i.minute.
Just thought I’d mention this in case anyone copies the example code into their program, and is confused by the incorrect timestamps…
How to interface keyboard as raw input for GUI Calc……?
nice explanation, however it only prints the time when you open the file, is there a way to make it so that the time updates?
I would use a while
There is a typo in the datetime examples. The last format has i.month instead of i.minute.
Really very helpful post
But how could I produce a code in which it records current time when ‘enter’ is pressed?
Also how can I take an amount of time away from another.
Helpful note, thank y
nice meme bro
It was really helpful. Thank you.
Thanks a lot