.datetime() method returns a new object with date and time properties.
datetime.datetime(YYYY, MM, DD, hh, mm, ss)
The following parameters must be valid when passed into the
||✓||The year expressed in one to four digits.||1 - 9999|
||✓||The month expressed in one or two digits.||1 - 12|
||✓||The day of the month expressed in one or two digits.||1 - the number of days in a given month and year|
||✕||The hour in a given 1-day period that can be expressed as one or two digits.||0 - 24|
||✕||The minute in a given 1-hour period that can be expressed as one or two digits.||0 - 60|
||✕||The second in a given 1-hour period that can be expressed as one or two digits.||0 - 60|
A date and time can be retrieved and stored in a variable as shown below:
Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.
Learn Python on Codecademy
Computer ScienceLooking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
Includes 6 Courses
With Professional Certification