You can use now() function of datetime python module. This Snippet will show you different ways to get the current date and time in the python best script. here the Below simple demo scripts to get date and time has been easy way to understanding and fully tested with Python version 2.7 and Python version 3.5 on your Linux system.
Get Current Date Time in Python
I can use Python datetime function of module to step by step get the current date and time of the local Linux system with example below.
import datetime getCurrentDateTime = datetime.datetime.now() print (str(getCurrentDateTime))
Let’s execute the script getCurrentDateTimePython1.py as following run this command.
$ python getCurrentDateTimePython1.py 2021-03-06 16:00:04.159338
Get Current Date Time Attributes in Python
You can also get the particular attribute of datetime. For example, if you want to get only current time, current year or month or datetime etc. make a getCurrentDateTimePython2.py file with following data source code, which will help you to step by step understand to fetch particular attribute.
import datetime getCurrentDateTime = datetime.datetime.now() print ("Current Year is: %d" % getCurrentDateTime.year) print ("Current Month is: %d" % getCurrentDateTime.month) print ("Current Day is: %d" % getCurrentDateTime.day) print ("Current Hour is: %d" % getCurrentDateTime.hour) print ("Current Minute is: %d" % getCurrentDateTime.minute) print ("Current Second is: %d" % getCurrentDateTime.second) print ("Current Microsecond is: %d" % getCurrentDateTime.microsecond)
And then, execute/run script getCurrentDateTimePython2.py like below
$ python getCurrentDateTimePython2.py Current Year is: 2021 Current Month is: 3 Current Day is: 6 Current Hour is: 16 Current Minute is: 6 Current Second is: 28
Get Formated Date Time in Python
Also, if you required date and time in a particular format, you can each directive to format datetime. Make getCurrentDateTimePython3.py file with following simple copy and paste source code. This example has some sample Python formatted datetime outputs.
import datetime getCurrentDateTime = datetime.datetime.now() print (getCurrentDateTime.strftime("%Y-%m-%d %H:%M:%S")) print (getCurrentDateTime.strftime("%Y/%m/%d")) print (getCurrentDateTime.strftime("%H:%M:%S")) print (getCurrentDateTime.strftime("%I:%M:%S %p")) print (getCurrentDateTime.strftime("%a, %b %d, %Y"))
Last step to run or execute getCurrentDateTimePython3.py from your command line
$ python getCurrentDateTimePython3.py 2021-03-06 16:13:59 2021/03/06 16:13:59 04:13:59 PM Mon, Mar 06, 2021
Python Current Date Time using pytz
Python simple pytz is one of the most Imp module that can be helps to get the full timezone scripts.so You can here simple install this Python module using the following simple execute or run this PIP command.
pip install pytz
examples of using pytz module to get time in particular time zones.
import pytz utc = pytz.utc pst = pytz.timezone('America/Los_Angeles') ist = pytz.timezone('Asia/Calcutta') print('Python Date Time in UTC =', datetime.now(tz=utc)) print('Python Date Time in PST =', datetime.now(pst)) print('Python Date Time in IST =', datetime.now(ist))
Python Date Time in UTC = 2022-10-11 08:57:18.110068+00:00 Python Date Time in PST = 2022-10-11 01:57:18.110106-07:00 Python Date Time in IST = 2022-10-11 14:27:18.110139+05:30
the following list of Python directives can be need to well format datetime results in your Python source code.
|%a||Locale’s abbreviated weekday name.|
|%A||Locale’s full weekday name.|
|%b||Locale’s abbreviated month name.|
|%B||Locale’s full month name.|
|%c||Locale’s appropriate datetime 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||Locale’s 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||Locale’s appropriate date representation.|
|%X||Locale’s appropriate time representation.|
|%y||Year without century as a decimal number [00,99].|
|%Y||Year with century as a decimal number.|
|%z||Time zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal hour digits and M represents decimal minute digits [-23:59, +23:59].|
|%Z||Time zone name (no characters if no time zone exists).|
|%%||A literal ‘%’ character.|
I hope you get an idea about how to print time in python.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.