How to get the current date and time in Python

In this tutorial, you will learn the classes provided by the DateTime module for manipulating dates and times in a simple and complex way in Python 3.x. of datetime Module contains datetime Class to get the current date and time you will use now() method.

Get the current time and date

To display the current time and date, we will import datetime From the class datetime Module. After that we will use now() Method of creating an 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 creation now Object from datetime Class, the object becomes a method with methods and properties that can help us set the year, month, day, hour, minute, second and microsecond respectively.

  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 use the following command to convert the date, time, and datetime objects to their equivalent strings 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 following table shows what you can pass to strftime() method:

Optionmeaningexample
%aAbbreviation for weekday name.Sunday, Monday …
%AFull name on weekdays.Sunday Monday, …
%wWeekdays are decimal numbers.0, 1, …, 6
%dDay of the month as a zero-padded decimal number.01, 02, …, 31
%-dThe day of the month as a decimal number.1, 2, …, 30
%bAbbreviated month name.January, February, …, December
%BFull month name.January, February, …
%mMonth as a zero-padded decimal number.01, 02, …, 12
%-mThe month as a decimal number.1, 2, …, 12
%yYear without century, zero-padded decimal number.00, 01, …, 99
%-yYear without century as decimal number.0, 1, …, 99
%YYear with century as decimal number.2013, 2019, etc.
%HHour (24-hour clock), a decimal number with zero padding.00, 01, …, 23
%-HHour (24-hour clock) is a decimal number.0, 1, …, 23
%IHour (12-hour clock), a zero-padded decimal number.01, 02, …, 12
%-IThe hour (12-hour clock) is a decimal number.1 2 2
%pLocale’s morning or afternoon.morning afternoon
%MMinute, a zero-padded decimal number.00, 01, …, 59
%-MRepresented as a decimal number.0, 1, …, 59
%SThe second is a zero-padded decimal number.00, 01, …, 59
%-SThe second decimal number.0, 1, …, 59
%fMicroseconds, decimal numbers, zero-padded on the left.000000-999999
%zUTC offset, format is + HHMM or -HHMM.
%ZTime zone name.
%jDay of the year as a zero-padded decimal number.001, 002, …, 366
%-jDay of the year as a decimal number.1, 2, …, 366
%UWeek of the year (Sunday is the first day of the week). All days of the New Year before the first Sunday are considered to be in week 0.00, 01, …, 53
%WWeek of the year (Monday is the first day of the week). All days in the new year before the first Monday are considered to be in week 0.00, 01, …, 53
%cThe appropriate date and time representation of the locale.Monday, September 30, 2013 07:06:05
%xThe appropriate date representation for the locale.13/9/30
%XThe appropriate time representation of the locale.07:06:05
%%Literal ‘%’ character.%

Get current time and date from timestamp

Timestamps are a very common method of storing dates and times in a database. Unix timestamps are the number of seconds between a UTC specific date and January 1, 1970.

  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() A method that returns the local date and time.

Get timestamp from current time and date

We will convert the current time and date to the timestamp we use now Object to get the current time and date, which is a convenient way to store the time and date in the database.

  timestamp = datetime.timestamp(now)
  print("timestamp =", timestamp)
output
timestamp = 1566417574.394412

Get the current time in the time zone

We will use pytz And module datetime The module gets the time in the time zone.

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

in conclusion

In this tutorial, we learned how to get the current date and time in Python. Hope you enjoy reading and leave your suggestions in the comments section below.

1) Datetime is a built-in library for Python that helps us deal with dates and times. 2) strftime Features allow us to control the time and date format. 3) The timestamp type allows us to store the time and date in the database.

Also read:

  • How to set up a Python virtual environment on Ubuntu 18.04
  • How to install the latest Python on Ubuntu 18.04
  • How to add items to a list using the Python Append () command
  • How to sort a list using the Python Sort List () method
Sidebar