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.
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.
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.
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.
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.
Locale’s abbreviated weekday name.
Locale’s full weekday name.
Locale’s abbreviated month name.
Locale’s full month name.
Locale’s appropriate datetime representation.
Day of the month as a decimal number [01,31].
Hour (24-hour clock) as a decimal number [00,23].
Hour (12-hour clock) as a decimal number [01,12].
Day of the year as a decimal number [001,366].
Month as a decimal number [01,12].
Minute as a decimal number [00,59].
Locale’s equivalent of either AM or PM.
Second as a decimal number [00,61].
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.
Weekday as a decimal number [0(Sunday),6].
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.
Locale’s appropriate date representation.
Locale’s appropriate time representation.
Year without century as a decimal number [00,99].
Year with century as a decimal number.
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].
Time zone name (no characters if no time zone exists).
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.