How to Get Current Date and Time in Python
Abstract: 31%-dDay of the month as a decimal number.1print("date and time
In this tutorial, you will learn the DateTime module supplies classes for manipulating dates and times in both simple and complex ways in Python 3.x. The datetime
module contains datetime
class, to get current date and time you will use now()
method.
To display the current time and date, We will import datetime
class from datetime
module. After that we will use now()
method to create object.
from datetime import datetime
now = datetime.now()
print("Current Time =", now)
output
Current Time = 2019-08-20 21:27:27.299378
Get current time and date attributes
After we created now
object from datetime
class, This object become has methods and attributes helps us to year, month, day, hour, minute, second and microsecond separately.
To view this video please enable JavaScript, and consider upgrading to a web browser that supports HTML5 video
Date & Time in Python (Printing and Calculating Date and Time) (Video 53) print ("Current Year is: %d" % now.year)
print ("Current Month is: %d" % now.month)
print ("Current Day is: %d" % now.day)
print ("Current Hour is: %d" % now.hour)
print ("Current Minute is: %d" % now.minute)
print ("Current Second is: %d" % now.second)
print ("Current Microsecond is: %d" % now.microsecond)
output
Current Year is: 2019
Current Month is: 8
Current Day is: 20
Current Hour is: 21
Current Minute is: 27
Current Second is: 27
Current Microsecond is: 299378
Get current time and date using strftime()
We will convert date, time and datetime objects to its equivalent string using strftime() methode
.
year = now.strftime("%Y")
print("year:", year)
month = now.strftime("%m")
print("month:", month)
day = now.strftime("%d")
print("day:", day)
time = now.strftime("%H:%M:%S")
print("time:", time)
date_time = now.strftime("%m/%d/%Y, %H:%M:%S")
print("date and time:",date_time)
output
year: 2019
month: 08
day: 20
time: 21:27:27
date and time: 08/20/2019, 21:27:27
Format date and time List
The table below shows all the options that you can pass to the strftime()
method:
%a
Abbreviated weekday name.Sun, Mon, ...%A
Full weekday name.Sunday, Monday, ...%w
Weekday as a decimal number.0, 1, ..., 6%d
Day of the month as a zero-padded decimal.01, 02, ..., 31%-d
Day of the month as a decimal number.1, 2, ..., 30%b
Abbreviated month name.Jan, Feb, ..., Dec%B
Full month name.January, February, ...%m
Month as a zero-padded decimal number.01, 02, ..., 12%-m
Month as a decimal number.1, 2, ..., 12%y
Year without century as a zero-padded decimal number.00, 01, ..., 99%-y
Year without century as a decimal number.0, 1, ..., 99%Y
Year with century as a decimal number.2013, 2019 etc.%H
Hour (24-hour clock) as a zero-padded decimal number.00, 01, ..., 23%-H
Hour (24-hour clock) as a decimal number.0, 1, ..., 23%I
Hour (12-hour clock) as a zero-padded decimal number.01, 02, ..., 12%-I
Hour (12-hour clock) as a decimal number.1, 2, ... 12%p
Locale’s AM or PM.AM, PM%M
Minute as a zero-padded decimal number.00, 01, ..., 59%-M
Minute as a decimal number.0, 1, ..., 59%S
Second as a zero-padded decimal number.00, 01, ..., 59%-S
Second as a decimal number.0, 1, ..., 59%f
Microsecond as a decimal number, zero-padded on the left.000000 - 999999%z
UTC offset in the form +HHMM or -HHMM. %Z
Time zone name. %j
Day of the year as a zero-padded decimal number.001, 002, ..., 366%-j
Day of the year as a decimal number.1, 2, ..., 366%U
Week number of the year (Sunday as the first day of the week). All days in a new year preceding the first Sunday are considered to be in week 0.00, 01, ..., 53%W
Week number of the year (Monday as the first day of the week). All days in a new year preceding the first Monday are considered to be in week 0.00, 01, ..., 53%c
Locale’s appropriate date and time representation.Mon Sep 30 07:06:05 2013%x
Locale’s appropriate date representation.09/30/13%X
Locale’s appropriate time representation.07:06:05%%
A literal '%' character.% Get current time and date from timestamp
Timestamp is a pretty common way to store date and time in a database. A Unix timestamp is the number of seconds between a particular date and January 1, 1970 at UTC.
timestamp = 1545730073
dt_object = datetime.fromtimestamp(timestamp)
print("dt_object =", dt_object)
print("type(dt_object) =", type(dt_object))
output
dt_object = 2018-12-25 01:27:53
type(dt_object) =
We used fromtimestamp()
method which returns the local date and time.
We will convert the current time and date to timestamp which we use now
object to get current time and date, This is an easy way to store time and date in a database.
timestamp = datetime.timestamp(now)
print("timestamp =", timestamp)
output
timestamp = 1566417574.394412
Get current time of a timezone
We will use pytz
module with datetime
module to get time of a timezone.
tz_Paris = pytz.timezone('Europe/Paris')
datetime_Paris = datetime.now(tz_Paris)
print("Paris time:", datetime_Paris.strftime("%H:%M:%S"))
output
Paris time: 22:30:05
Conclusion
In this tutorial, we learned how to get the current date and time in Python. I hope you enjoyed reading and please leave your suggestion in the below comment section.
1) Datetime is built-in Python's library and helps us to deal with date and time.
2) The strftime
function allows us to control on time and date format.
3) The timestamp type allows us to store time and date in the database.
- How to Setup Python Virtual Environment on Ubuntu 18.04
- How to Install Latest Python on Ubuntu 18.04
- How to Add an Item in a List Using Python Append() Command
- How to Sort a List Using Python Sort List() Method