Javaexercise.com

Python Date Object

Date is an object in Python which is used to deal with date and related functions. The Python Date object belongs to datetime module which provides other supporting objects as well.

The date represents combination of year, month and day in an idealized calendar manner.

To create date in Python, we can use Date constructor that takes three parameters. Syntax of date constructure is given below.

Python Date Syntax

Date (year, month, day)

All the arguments are required and must be integers. All these arguments must follow ranges and constraints which are given below.

Date Parameter Constraints:

  • year must be >= MINYEAR AND <= MAXYEAR

  • month must be >= 1 AND <= 12

  • day must be >= 1 AND <= number of days in the given month and year

 

MINYEAR and MAXYEAR both are constants belong to datetime module. The default value of both constants are 1 and 9999 respectively.

The date object raised an error ValueError If an argument outside those ranges is given.

Python Date Example : Creating a Date

In the below example, we are creating a date by providing required arguments. It returns date in y-m-d format. See the below example.

from datetime import date

# fetch date
date_obj = date(2018,3,15)

print("The given date : ",date_obj)

# printing type of object
print(type(date_obj))

Output:

The given date :  2018-03-15
<class 'datetime.date'>
 

Apart from creating date, date module contains other useful methods and attributes as well. A list of methods and attributes is given below.

Python Date Object Attributes

Attribute Description
min It returns minimum date.
max It returns maximum date, date(MAXYEAR, 12, 31).
resolution The smallest possible difference between non-equal date objects, timedelta(days=1).
year It returns year between MINYEAR and MAXYEAR inclusive.
month It returns month between 1 and 12 inclusive.
day Returns number of day in the date.

Example: Python Date Object Attributes

In this example, we are using date attributes to fetch information about the date.

from datetime import date

date_obj = date(2018,2,15)

# Min attribute
print("Min Date : ",date_obj.min)

# Max attribute
print("Max Date : ",date_obj.max)

# Year attribute
print("Year : ",date_obj.year)

# Month attribute
print("Month : ",date_obj.month)

# Day attribute
print("Day : ",date_obj.day)

Output:

Min Date :  0001-01-01
Max Date :  9999-12-31
Year :  2018
Month :  2
Day :  15
 

Python Date Object Methods

Method Description
today() It returns current local date.
fromtimestamp(timestamp) It returns the local date corresponding to the POSIX timestamp.
fromordinal(ordinal) It is used to get the date corresponding to the proleptic Gregorian ordinal.
fromisoformat(date_string) It returns a date corresponding to a date_string .
fromisocalendar(year, week, day) It is used to get a date corresponding to the ISO calendar.
replace(year=self.year, month=self.month, day=self.day) It is used to get a date with the same value.
timetuple() It returns a time.struct_time 
toordinal() It returns the proleptic Gregorian ordinal of the date.
weekday() It is used to get the day of the week as an integer.
isoweekday() It is used to get day of the week as an integer, where Monday is 1 and Sunday is 7.
isocalendar() It returns a 3-tuple, (ISO year, ISO week number, ISO weekday).
isoformat() It returns a string representing the date in ISO 8601 format, YYYY-MM-DD.
ctime() It is used to get a string representing the date.
strftime(format) It returns a string representing the date.

Conclusion

In this tutorial, we learnt about Python date object and its methods. In our next topic we will cover other object of the datetime module. Till then keep practice.

Current Date and Time in Python