ADVERTISEMENT
ADVERTISEMENT

Python datetime date() Method with Example

Python datetime.date() Method: Here, we are going to learn about the date() method of datetime class in Python with its definition, syntax, and examples.
Submitted by Hritika Rajput, on May 03, 2020

Python datetime.date() Method

datetime.date() method is used to manipulate objects of datetime class of module datetime.

It uses an instance method and converts it into a date object with the same year, month and day. We require an instance of datetime class as it is an instance method.

Module:

    import datetime

Class:

    from datetime import datetime

Syntax:

    date()

Parameter(s):

  • None

Return value:

The return type of this method is a date object with year, month and day value same as that of the given datetime object.

Example:

## Creating a date object from a datetime object
from datetime import datetime

## Creating datetime instance
x = datetime(2020, 3, 4,23,12,23,44)
d = x.date()
print("Original object:", x)
print("New date object:",d)
print()

x = datetime.now()
d = x.date()
print("Original date and time", x)
print("New date objec:", d)
print()

x = datetime.today()
d = x.date()
print("Printing the same")
print(x)
print(d)

Output

Original object: 2020-03-04 23:12:23.000044
New date object: 2020-03-04

Original date and time 2020-05-03 17:08:13.516192
New date objec: 2020-05-03

Printing the same
2020-05-03 17:08:13.517060
2020-05-03
ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.