Date class in Python

Overview

The date class, in Python represents a date as per Gregorian calendar. Pope Gregory XIII introduced Gregorian calendar in the month of October, year 1582.

The date class in Python represents dates before the introduction of Gregorian calendar as well using the same Gregorian conventions.

Remember that prior to Gregorian calendar, the calendar convention followed was Julian calendar.

However, the date class in Python represents dates from day 1 of the year from the infinite past to till date and all future dates using the convention of Gregorian calendar.

A calendar that uses Gregorian convention for representing dates before 1582 is called Proleptic Gregorian calendar. Hence, Python date class represents a Proleptic Gregorian calendar.

As per Python's date class, day 1 of year gets an ordinal of 1 and day 2 of the year 1 gets an ordinal of 2 and so on. These ordinals are day numbers and are called as Proleptic Gregorian Ordinals.

Objects of date class in Python do not store neither time zone information nor Daylight Saving information. Hence the date objects are called as "naive" objects.

Creating a date object in Python:

In Python, objects of type datetime.date can be created using the constructor datetime.date. The function datetime.today as well can be used to create a date object, which returns a date object from current time.

Date objects can be created using a POSIX timestamp as well.

The class function datetime.fromtimestamp() creates a date object from a POSIX timestamp. A POSIX timestamp is number of seconds elapsed from Unix Epoch. Unix Epoch starts from 00:00:00 UTC on 1 January 1970.

As seen from the overview section, every date has a corresponding Proleptic Gregorian Ordinal, which is nothing but the day count from the day 1 of the year 1. A Python naive date object can be created using this Gregorian Ordinal as well, by using the class function fromordinal().

Example for creating date objects in Python:

import datetime

import time

 

# creates a date object with day 1, month 1 and year 1

date1 = datetime.date(1, 1, 1)

print("Day 1, Month 1, Year 1 :{}".format(date1))

 

# creates a Python's version 1 birthday date of January 26, 1994

pythonsBirthDay = datetime.date(1994, 1, 26)

print("Python v1.0.0 was released on {}".format(pythonsBirthDay))

 

# creates a Python's version 3.6 birthday date of 2016-12-23

pythonsLatestBirthDay = datetime.date(2016, 12, 23)

print("Python v3.6.0 was released on {}".format(pythonsLatestBirthDay))

 

# creates a date object with current date

todaysDate = datetime.date.today()

print("Today's date is {}".format(todaysDate))

 

#create a date object from POSIX time stamp

currentTimeStamp    = time.time()

dateFromTS          = datetime.date.fromtimestamp(currentTimeStamp)

print("Date from POSIX timestamp {}".format(dateFromTS))

 

#create a date object from Proleptic Gregorian Ordinal 100K

dateOnMillionthDay = datetime.date.fromordinal(1000000)

print("Date on millionth day is {}".format(dateOnMillionthDay))

 

 

Output:

Day 1, Month 1, Year 1 :0001-01-01

Python v1.0.0 was released on 1994-01-26

Python v3.6.0 was released on 2016-12-23

Today's date is 2017-02-09

Date from POSIX timestamp 2017-02-09

Date on millionth day is 2738-11-28

 


Copyright 2024 © pythontic.com