Python .datetime()

BrandonDusch's avatar
Published Jun 7, 2022
Contribute to Docs

The .datetime() method returns a new object with date and time properties.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours

Syntax

datetime.datetime(YYYY, MM, DD, hh, mm, ss)

The following parameters must be valid when passed into the .datetime() method:

Parameter Required? Description Range
YYYY ✓ The year expressed in one to four digits. 1 - 9999
MM ✓ The month expressed in one or two digits. 1 - 12
DD ✓ The day of the month expressed in one or two digits. 1 - the number of days in a given month and year
hh The hour in a given 1-day period that can be expressed as one or two digits. 0 - 24
mm The minute in a given 1-hour period that can be expressed as one or two digits. 0 - 60
ss The second in a given 1-hour period that can be expressed as one or two digits. 0 - 60

Codebyte Example

A date and time can be retrieved and stored in a variable as shown below:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn Python on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours