![[Formales]] ![[strftime__451361492.png]] [Python strftime()](https://www.programiz.com/python-programming/datetime/strftime)<br> From: *Programiz* > [!TLDR] String Format Time > - strftime stands for **string format time**, and it's a function commonly used to format time and date information. The function takes a format string and a time object as arguments and returns a string where various time-related components are formatted according to the format string. > - The format string consists of various placeholders, each starting with a **%** symbol, that specify how the corresponding component of the time should be displayed. These placeholders are replaced by the actual values when the function is executed. # Common Placeholders - `%Y`: Year with century (e.g., "2023") - `%m`: Month as a zero-padded decimal (e.g., "09" for September) - `%d`: Day of the month as a zero-padded decimal (e.g., "07") - `%H`: Hour (24-hour clock) as a zero-padded decimal (e.g., "14") - `%M`: Minute as a zero-padded decimal (e.g., "05") - `%S`: Second as a zero-padded decimal (e.g., "09") - `%A`: Full weekday name (e.g., "Monday") - `%a`: Abbreviated weekday name (e.g., "Mon") - `%B`: Full month name (e.g., "September") - `%b`: Abbreviated month name (e.g., "Sep") # More Placeholders - `%y`: Year without century (e.g., "23" for 2023) - `%I`: Hour (12-hour clock) as a zero-padded decimal (e.g., "02") - `%p`: Locale’s equivalent of AM/PM (e.g., "AM", "PM") - `%f`: Microsecond as a zero-padded decimal (e.g., "000001") - `%z`: UTC offset in the form ±HHMM[SS[.ffffff]] (e.g., "+0100") - `%Z`: Time zone name (e.g., "EST") - `%j`: Day of the year as a zero-padded decimal (e.g., "250" for September 7th) - `%U`: Week number of the year (Sunday as the first day of the week) as a zero-padded decimal (e.g., "36") - `%W`: Week number of the year (Monday as the first day of the week) as a zero-padded decimal (e.g., "36") - `%c`: Locale’s appropriate date and time representation (e.g., "Mon Sep 7 14:05:09 2023") - `%x`: Locale’s appropriate date representation (e.g., "09/07/23") - `%X`: Locale’s appropriate time representation (e.g., "14:05:09") - `%%`: A literal '%' character (e.g., "%") # Introductory Examples ```python from datetime import datetime # Current date and time now = datetime.now() # Format the datetime object formatted_time = now.strftime("%Y-%m-%d %H:%M:%S") # Another example formatted_time = today.strftime("%A, %B %d, %Y") ``` # Exercises ![[Strftime - Cookie Company#Overview]]