🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The strftime function in Python's time module formats a struct_time object or a tuple representing a time as a string according to a specified format. This function is useful for creating human-readable representations of time and date.
Table of Contents
- Introduction
strftimeFunction Syntax- Examples
- Basic Usage
- Formatting Current Time
- Real-World Use Case
- Conclusion
Introduction
The strftime function in Python's time module converts a struct_time object or a tuple representing time to a formatted string. This is useful for displaying time and date information in a human-readable format.
strftime Function Syntax
Here is how you use the strftime function:
import time
time.strftime(format, t)
Parameters:
format: A string specifying the desired format of the output. It can include various format codes to represent different parts of the date and time.t: An optionalstruct_timeobject or a tuple representing time. If not provided, the current time is used.
Returns:
- A string representing the formatted time.
Common Format Codes:
%Y: Year with century (e.g., 2021)%m: Month as a zero-padded decimal number (e.g., 08)%d: Day of the month as a zero-padded decimal number (e.g., 17)%H: Hour (24-hour clock) as a zero-padded decimal number (e.g., 15)%M: Minute as a zero-padded decimal number (e.g., 43)%S: Second as a zero-padded decimal number (e.g., 06)%A: Full weekday name (e.g., Tuesday)%B: Full month name (e.g., August)
Examples
Basic Usage
Here is an example of how to use strftime.
Example
import time
# Creating a struct_time object representing a specific time
time_struct = time.struct_time((2021, 8, 17, 15, 43, 6, 1, 229, 1))
# Formatting the time as a string
formatted_time = time.strftime("%Y-%m-%d %H:%M:%S", time_struct)
print("Formatted time:", formatted_time)
Output:
Formatted time: 2021-08-17 15:43:06
Formatting Current Time
This example shows how to format the current time as a string using strftime.
Example
import time
# Getting the current local time as a struct_time object
current_time_struct = time.localtime()
# Formatting the current time as a string
formatted_current_time = time.strftime("%Y-%m-%d %H:%M:%S", current_time_struct)
print("Current formatted time:", formatted_current_time)
Output:
Current formatted time: 2024-07-23 20:33:47
Real-World Use Case
Creating Timestamps for Logging
In real-world applications, the strftime function can be used to create formatted timestamps for logging events.
Example
import time
def log_event(event):
current_time_struct = time.localtime()
timestamp = time.strftime("%Y-%m-%d %H:%M:%S", current_time_struct)
print(f"[{timestamp}] Event: {event}")
# Example usage
log_event("Start process")
time.sleep(2)
log_event("End process")
Output:
[2024-07-23 20:33:47] Event: Start process
[2024-07-23 20:33:49] Event: End process
Conclusion
The strftime function in Python's time module formats a struct_time object or a tuple representing time as a string according to a specified format, making it useful for creating human-readable representations of time and date.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment