.datetime.now()
Published Jun 7, 2022
Contribute to Docs
The .datetime.now()
method returns a new datetime
object with the current local date and timestamp.
Syntax
datetime.datetime.now(time_zone=None)
The result from the .datetime.now()
method contains the year, month, day, hour, minute, second, and microsecond (expressed as YYYY-MM-DD hh:mm:ss.ffffff
).
It also accepts an optional time_zone
parameter, which is set to None
by default.
Codebyte Example
The current date and time can be retrieved and stored in a variable as shown below:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours