Additional Date Module in Python
Additional Date Module in Python
I. strftime() is a method in Python's datetime module that stands for "string format time." It is
used to format datetime objects into readable strings based on specified format codes. This
method allows you to convert a datetime object into a string representation that can be
customized according to your needs.
Syntax: datetime_object.strftime(format)
Here are some commonly used format codes with strftime():
• %Y: Year with century as a decimal number (e.g., 2023).
• %y: Year without century as a zero-padded decimal number (e.g., 23 for 2023).
• %m: Month as a zero-padded decimal number (01 to 12).
• %B: Full month name (e.g., January, February).
• %b: Abbreviated month name (e.g., Jan, Feb).
• %d: Day of the month as a zero-padded decimal number (01 to 31).
• %A: Full weekday name (e.g., Monday, Tuesday).
• %a: Abbreviated weekday name (e.g., Mon, Tue).
• %H: Hour (00 to 23).
• %I: Hour (01 to 12).
• %M: Minute (00 to 59).
• %S: Second (00 to 59).
• %p: AM or PM.
II. strptime() is a method in Python's datetime module that stands for "string parse time." It is used
to create a datetime object from a string representation of a date and/or time, based on a
specified format. This method is particularly useful when you have date and time information in
string format and you want to convert it into a datetime object for further manipulation or
calculations.
Syntax: datetime.strptime(date_string, format)
Here are some commonly used format codes with strptime():
• %Y: Year with century as a decimal number (e.g., 2023).
• %y: Year without century as a zero-padded decimal number (e.g., 23 for 2023).
• %m: Month as a zero-padded decimal number (01 to 12).
• %d: Day of the month as a zero-padded decimal number (01 to 31).
• %H: Hour (00 to 23).
• %I: Hour (01 to 12).
• %M: Minute (00 to 59).
• %S: Second (00 to 59).
• %p: AM or PM.
• %B: Full month name (e.g., January).
• %b: Abbreviated month name (e.g., Jan).