Python Basic: Exercise-3 with Solution
Write a Python program to display the current date and time.
Python datetime:
The datetime module supplies classes for manipulating dates and times in both simple and complex ways. datetime.now[tz=None] returns the current local date and time. If optional argument tz is None or not specified, this is like today[].
date.strftime[format] returns a string representing the date, controlled by an explicit format string. Format codes referring to hours, minutes or seconds will see 0 values.
Sample Solution:-
Python Code:
import datetime
now = datetime.datetime.now[]
print ["Current date and time : "]
print [now.strftime["%Y-%m-%d %H:%M:%S"]]
Sample Output:
Current date and time : 2014-07-05 14:34:14
Flowchart:
Visualize Python code execution:
The following tool visualize what the computer is doing step-by-step as it executes the said program:
Python Code Editor:
Have another way to solve this solution? Contribute your code [and comments] through Disqus.
Previous: Write a Python program to get the Python version you are using.
Next: Write a Python program which accepts the radius of a circle from the user and compute the area.
Python: Tips of the Day
Summing a sequence of numbers [calculating the sum of zero to ten with skips]:
>>> l = range[0,10,2] >>> sum[l] 20
In this article, you will learn to get today's date and current date and time in Python. We will also format the date and time in different formats using strftime[] method.
Video: Dates and Times in Python
There are a number of ways you can take to get the current date. We will use the
date
class of the datetime module to accomplish this task.
Example 1: Python get today's date
from datetime import date
today = date.today[]
print["Today's date:", today]
Here, we imported the date
class from the datetime
module. Then, we used the date.today[]
method to get the current local date.
By the way, date.today[]
returns a date
object, which is assigned to the today variable in the above
program. Now, you can use the strftime[] method to create a string representing date in different formats.
Example 2: Current date in different formats
from datetime import date
today = date.today[]
# dd/mm/YY
d1 = today.strftime["%d/%m/%Y"]
print["d1 =", d1]
# Textual month, day and year
d2 = today.strftime["%B %d, %Y"]
print["d2 =", d2]
# mm/dd/y
d3 = today.strftime["%m/%d/%y"]
print["d3 =", d3]
# Month abbreviation, day and year
d4 = today.strftime["%b-%d-%Y"]
print["d4 =", d4]
When you run the program, the output will be something like:
d1 = 16/09/2019 d2 = September 16, 2019 d3 = 09/16/19 d4 = Sep-16-2019
If you need to get the current date and time, you can use datetime
class of the datetime
module.
Example 3: Get the current date and time
from datetime import datetime
# datetime object containing current date and time
now = datetime.now[]
print["now =", now]
# dd/mm/YY H:M:S
dt_string = now.strftime["%d/%m/%Y %H:%M:%S"]
print["date and time =", dt_string]
You will gate output like below.
now = 2021-06-25 07:58:56.550604 date and time = 25/06/2021 07:58:56
Here, we have used datetime.now[]
to get the current date and time. Then, we used strftime[]
to create a string representing date and time in another format.
In this article, we will cover different methods for getting data and time using the DateTime module and time module in Python.
Different ways to get Current Date and Time using Python
- Current time using DateTime object
- Get time using the time module
Method 1: Using the Datetime Module
In this example, we will learn How to get the current Date and Time using Python. In Python, date and time are not data types of their own, but a module named DateTime can be imported to work with the date as well as time. Datetime module comes built into Python, so there is no need to install it externally. To get both current date and time datetime.now[] function of DateTime module is used. This function returns the current local date and time.
Example 1: Current date-time using DateTime
In this example, we will use the DateTime object for getting the date and time using datetime.now[].
Python3
import
datetime
current_time
=
datetime.datetime.now[]
print
[
"Time now at greenwich meridian is:"
, current_time]
Output:
Time now at greenwich meridian is: 2022-06-20 16:06:13.176788
Example 2: Attributes of DateTime using DateTime
timedate.now[] have different attributes, same as attributes of time such as year, month, date, hour, minute, and second.
Python3
import
datetime
current_time
=
datetime.datetime.now[]
print
[
"The attributes of now[] are :"
]
print
[
"Year :"
, current_time.year]
print
[
"Month : "
, current_time.month]
print
[
"Day : "
, current_time.day]
print
[
"Hour : "
, current_time.hour]
print
[
"Minute : "
, current_time.minute]
print
[
"Second :"
, current_time.second]
print
[
"Microsecond :"
, current_time.microsecond]
Output:
The attributes of now[] are : Year : 2022 Month : 6 Day : 20 Hour : 16 Minute : 3 Second : 25 Microsecond : 547727
Example 3: Get a particular timezone using pytz and datetime
In this example, it can be seen that the above code does not give the current date and time of your timezone. To get the date and time for a particular timezone now[] takes timezone as input to give timezone-oriented output time. But these time zones are defined in pytz library.
Python3
import
datetime
import
pytz
current_time
=
datetime.datetime.now[pytz.timezone[
'Asia/Kolkata'
]]
print
[
"The current time in india is :"
, current_time]
Output:
The current time in india is : 2019-12-11 19:28:23.973616+05:30
Example 4: Get Current Time In UTC using datetime
UTC Stands for Coordinated Universal Time, These times are useful when you are dealing with Applications that have a global user for logging the events. You can get the current time in UTC by using the datetime.utcnow[] method
Python3
from
datetime
import
datetime
print
[
"UTC Time: "
, datetime.utcnow[]]
Output:
UTC Time: 2022-06-20 11:10:18.289111
Example 5: Get Current Time in ISO Format using datetime
isoformat[] method is used to get the current date and time in the following format: It starts with the year, followed by the month, the day, the hour, the minutes, seconds, and milliseconds.
Python3
from
datetime
import
datetime as dt
x
=
dt.now[].isoformat[]
print
[
'Current ISO:'
, x]
Output:
Current ISO: 2022-06-20T17:03:23.299672
Method 2: Using the time Module
The Python time module, allows you to work with time in Python. It provides features such as retrieving the current time, pausing the program’s execution, and so on. So, before we begin working with this module, we must first import it.
Example 1: Get the current time using the time
Here, we are getting the current time using the time module.
Python3
import
time
curr_time
=
time.strftime[
"%H:%M:%S"
, time.localtime[]]
print
[
"Current Time is :"
, curr_time]
Output:
Current Time is : 16:19:13
Example 2: Get Current Time In Milliseconds using time
Here we are trying to get time in milliseconds by multiplying time by 1000.
Python3
import
time
millisec
=
int
[
round
[time.time[]
*
1000
]]
print
[
"Time in Milli seconds: "
, millisec]
Output:
Time in Milli seconds: 1655722337604
Example 3: Get Current Time In Nanoseconds using time
In this example, we will get time in nanoseconds using time.ns[] method.
Python3
import
time
curr_time
=
time.strftime[
"%H:%M:%S"
, time.localtime[]]
print
[
"Current Time is :"
, curr_time]
nano_seconds
=
time.time_ns[]
print
[
"Current time in Nano seconds is : "
, nano_seconds]
Output:
Current Time is : 16:26:52 Current time in Nano seconds is : 1655722612496349800
Example 4: Get Current GMT Time using time
Green Mean Time, which is also known as GMT can be used by using time.gmtime[] method in python just need to pass the time in seconds to this method to get the GMT
Python3
import
time
gmt_time
=
time.gmtime[time.time[]]
print
[
'Current GMT Time:\n'
, gmt_time]
Output:
Current GMT Time: time.struct_time[tm_year=2022, tm_mon=6, tm_mday=20, tm_hour=11, tm_min=24, tm_sec=59, tm_wday=0, tm_yday=171, tm_isdst=0]
Example 5: Get Current Time In Epoch using time
It is mostly used in file formats and operating systems. We can get the Epoch current time by converting the time.time[] to an integer.
Python3
import
time
print
[
"Epoch Time is : "
,
int
[time.time[]]]
Output:
Epoch Time is : 1655723915