How to Get Current Date and Time in Python

Channel: Linux
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.

Get current time and date

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.

Date & Time in Python (Printing and...

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:

OptionMeaningExample%aAbbreviated weekday name.Sun, Mon, ...%AFull weekday name.Sunday, Monday, ...%wWeekday as a decimal number.0, 1, ..., 6%dDay of the month as a zero-padded decimal.01, 02, ..., 31%-dDay of the month as a decimal number.1, 2, ..., 30%bAbbreviated month name.Jan, Feb, ..., Dec%BFull month name.January, February, ...%mMonth as a zero-padded decimal number.01, 02, ..., 12%-mMonth as a decimal number.1, 2, ..., 12%yYear without century as a zero-padded decimal number.00, 01, ..., 99%-yYear without century as a decimal number.0, 1, ..., 99%YYear with century as a decimal number.2013, 2019 etc.%HHour (24-hour clock) as a zero-padded decimal number.00, 01, ..., 23%-HHour (24-hour clock) as a decimal number.0, 1, ..., 23%IHour (12-hour clock) as a zero-padded decimal number.01, 02, ..., 12%-IHour (12-hour clock) as a decimal number.1, 2, ... 12%pLocale’s AM or PM.AM, PM%MMinute as a zero-padded decimal number.00, 01, ..., 59%-MMinute as a decimal number.0, 1, ..., 59%SSecond as a zero-padded decimal number.00, 01, ..., 59%-SSecond as a decimal number.0, 1, ..., 59%fMicrosecond as a decimal number, zero-padded on the left.000000 - 999999%zUTC offset in the form +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 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%WWeek 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%cLocale’s appropriate date and time representation.Mon Sep 30 07:06:05 2013%xLocale’s appropriate date representation.09/30/13%XLocale’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.

Get timestamp from current time and date

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.

Read Also:
  • 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

Ref From: linoxide
Channels:

Related articles