Date and Time functions

The datetime , time and calendar modules provides functions and classes for manipulating dates and times. However for general purpose parsing, formatting and arithmetic, we only use datetime module. We will learn only datetime module in this lesson. We use date, datetime and time objects from datetime module for most of the date manipulations

A date object from datetime module represents a date (year, month and day) in a Gregorian calendar.

The time object represents hour, minute, second and can also include time zone information.

A datetime object represents both date and time.

import datetime
today = datetime.date.today()
now = datetime.datetime.now()
print(today)
print(now)

Output:

Date and time when this program is run in yyyy-mm-dd format for date and yyyy-mm-dd hours:minutes:seconds format for datetime

Above program prints out date and time when the program is run. You can format the display using strftime function. Also you can import just datetime and date from datetime module insteading of importing all the functions and classes from datetime. Here is the modified program

from datetime import datetime
from datetime import date

today = date.today() # no need of datetime prefix due to date getting a namespace in import
now = datetime.now()
print(today.strftime("%m/%d/%Y"))
print(now.strftime("%m/%d/%Y %H:%M"))

Output:

Prints out the date, datetime in the given format of month/day/year for date part and for the time part it removes the seconds value.

Few routinely used formatting codes.

Code Description Example
%y 2-digit year 99
%Y 4-digit year 1999
%H Hour in 24 hour format 13
%I Hour in 12 hour format 02
%B Month name in full March
%b Abbreviated month name Mar
%A Full weekday name Friday
%a Abbreviated weekday name Fri

Create new date objects:

Python provides many constructors to create a date, datetime object from a string.

What is a constructor

A constructor in object oriented programming is a structure similar to a function which when invoked by sending in required parameters will return an object of the type that the contructor belongs to. Using date constructor we create date objects. Similarly using constructor for datetime you construct a datetime object.

Create datetime object

Using constructors for datetime and date, you can create respective objects from strings. datetime type can be either naive or aware. naive objects do not hold the timezone information. But since date object does not hold time information, date object does not have an aware version

datetime(year, month, day [,hour][,min][,sec][,microsec]) - datetime naive constructor

datetime (year, month, day [,hour][,min][,sec][,microsec][,tzinfo]) - datetime aware constructor (with timezone)

Reference the official documentation for tzinfo: https://docs.python.org/3/library/datetime.html#tzinfo-objects

If you have to create a datetime for a log entry on Oct 1st 1990, you would do so with below code:

log_entry = date(1990,10,1)

You can use strptime function on datetime to parse a string which represents date and time, into a date object using multiple formats. Here are the different ways you could parse the strings which represent date/time in many formats, to get the same datetime object value.

datetime.strptime("01/10/1990 12:10:03", "%d/%m/%Y %H:%M:%S")
datetime.strptime("01 10 1990 12-10-03", "%d %m %Y %H-%M-%S")
datetime.strptime("01 10 1990 12 10 03", "%d %m %Y %H %M %S")

You can get various parts of the date/datetime by accessing its attributes.

from datetime import datetime
from datetime import date

log_entry = datetime(1990,10,1,12,10,3)
print(log_entry)

print(log_entry.month)  # Outputs 10
print(log_entry.year)   # Outputs 1990
print(log_entry.day)    # Outputs 1
print(log_entry.hour)   # Outputs 12
print(log_entry.minute) # Outputs 10
print(log_entry.second) # Outputs 3

Output:

1990-10-01 12:10:03
10
1990
1
12
10
3

results matching ""

    No results matching ""