0% found this document useful (0 votes)
2 views14 pages

Python Datetime Module Summary

H

Uploaded by

stamstam167
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views14 pages

Python Datetime Module Summary

H

Uploaded by

stamstam167
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Introduction to datetime module

• Provides classes for working with date and


time.
• Used in event logging, database tracking, data
validation, and storing timestamps.
• Essential for real-world applications like
banking, scheduling, and logging.
Getting the current date & Creating date
objects

• Use datetime.date.today()
to get the current local
date.
• Attributes: year, month,
day (read-only).
• Create date: date(year,
month, day).
Creating date from timestamp

• The date class gives us the ability to create a date object from
a timestamp.
• Timestamp = seconds since Jan 1, 1970 (Unix epoch).

• Use date.fromtimestamp() with time.time().


Creating date with ISO format
• Use date.fromisoformat('YYYY-MM-DD') for ISO format.
replace() and weekday methods

• replace() creates a new date with modified year, month, or day.

• weekday(): 0 = Monday, 6 = Sunday.

• isoweekday(): 1 = Monday, 7 = Sunday (ISO 8601).


Creating time objects
• Use time(hour, minute, second, microsecond).
• Attributes: hour, minute, second, microsecond.
The time module & sleep function

• time.sleep(seconds) suspends program execution.


• Useful for simulating delays or pauses in execution.
ctime(), gmtime(), and localtime()

• ctime(): Converts timestamp to readable string.

• gmtime(): Returns UTC struct_time.


• localtime(): Returns local struct_time.
asctime() and mktime()

• asctime(): Converts struct_time to a string.


• mktime(): Converts struct_time to seconds
since epoch.
Creating datetime objects

• datetime(year, month, day, hour, minute, second).


• Combines date and time in one object.
• Methods:
• date() returns date,
• time() returns time.
Getting current datetime

• today(): Current local date and time (tzinfo=None).


• now(): Same as today(), optional tz argument.
• utcnow(): Current UTC date and time.
Timestamps & Formatting

• timestamp(): Returns seconds since epoch as float.


• strftime(): Format date/time using directives (%Y, %m, %d,
etc.).
• Example:
strptime() and timedelta

• strptime(): Parses string to datetime using format.


• timedelta: Represents duration (days, seconds,
microseconds).
• Supports arithmetic with date and datetime objects.
timedelta operations

• Supports addition, subtraction, and


multiplication.
• Can calculate differences between two
dates/times.
• Internally stores days, seconds, and
microseconds.

You might also like