🎓 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 datetime.utcnow function in Python's datetime module returns the current UTC (Coordinated Universal Time) date and time. This function is useful for capturing the current time in a timezone-independent manner.
Table of Contents
- Introduction
datetime.utcnowFunction Syntax- Examples
- Basic Usage
- Formatting Current UTC Date and Time
- Real-World Use Case
- Conclusion
Introduction
The datetime.utcnow function in Python's datetime module retrieves the current UTC date and time as a datetime object. This is useful for applications that need to work with a standardized time regardless of the local timezone.
datetime.utcnow Function Syntax
Here is how you use the datetime.utcnow function:
from datetime import datetime
current_utc_datetime = datetime.utcnow()
Parameters:
- The
datetime.utcnowfunction does not take any parameters.
Returns:
- A
datetimeobject representing the current UTC date and time.
Examples
Basic Usage
Here is an example of how to use datetime.utcnow.
Example
from datetime import datetime
# Getting the current UTC date and time
current_utc_datetime = datetime.utcnow()
print("Current UTC date and time:", current_utc_datetime)
Output:
C:\Users\rames\AppData\Local\Temp\script1240902829014802343.py:4: DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC).
current_utc_datetime = datetime.utcnow()
Current UTC date and time: 2024-07-23 15:05:12.266651
Formatting Current UTC Date and Time
This example shows how to format the current UTC date and time using the strftime method of the datetime object.
Example
from datetime import datetime
# Getting the current UTC date and time
current_utc_datetime = datetime.utcnow()
# Formatting the current UTC date and time
formatted_utc_datetime = current_utc_datetime.strftime("%Y-%m-%d %H:%M:%S")
print("Formatted current UTC date and time:", formatted_utc_datetime)
Output:
C:\Users\rames\AppData\Local\Temp\script15791210165026335996.py:4: DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC).
current_utc_datetime = datetime.utcnow()
Formatted current UTC date and time: 2024-07-23 15:05:12
Real-World Use Case
Logging Events in UTC
In real-world applications, the datetime.utcnow function can be used to log events with timestamps in UTC, which is useful for consistency across different time zones.
Example
from datetime import datetime
import time
def log_event(event):
timestamp = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
print(f"[{timestamp} UTC] Event: {event}")
# Example usage
log_event("Start process")
time.sleep(2)
log_event("End process")
Output:
C:\Users\rames\AppData\Local\Temp\script14232235721230879453.py:5: DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC).
timestamp = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
[2024-07-23 15:05:12 UTC] Event: Start process
[2024-07-23 15:05:14 UTC] Event: End process
Conclusion
The datetime.utcnow function provides the current UTC date and time as a datetime object, making it useful for applications that require a standardized time regardless of local timezone differences.
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