GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — Python datetime - Working with Dates and Times Python datetimelast modified February 15, 2025
In this article, we show how to use the
The Basic Usage of datetime
The following example demonstrates how to use the main.py
from datetime import datetime
# Get the current date and time
now = datetime.now()
print("Current Date and Time:", now)
# Access individual components
print("Year:", now.year)
print("Month:", now.month)
print("Day:", now.day)
print("Hour:", now.hour)
print("Minute:", now.minute)
print("Second:", now.second)
In this program, the $ python main.py Current Date and Time: 2025-02-15 12:34:56.789012 Year: 2025 Month: 2 Day: 15 Hour: 12 Minute: 34 Second: 56 Formatting Dates and Times
The following example demonstrates how to format dates and times using the
main.py
from datetime import datetime
# Get the current date and time
now = datetime.now()
# Format the date and time
formatted_date = now.strftime("%Y-%m-%d")
formatted_time = now.strftime("%H:%M:%S")
formatted_datetime = now.strftime("%Y-%m-%d %H:%M:%S")
print("Formatted Date:", formatted_date)
print("Formatted Time:", formatted_time)
print("Formatted Date and Time:", formatted_datetime)
In this program, the $ python main.py Formatted Date: 2025-02-15 Formatted Time: 12:34:56 Formatted Date and Time: 2025-02-15 12:34:56 Parsing Dates and Times
The following example demonstrates how to parse date strings into
main.py
from datetime import datetime
# Parse a date string
date_string = "2025-02-15 12:34:56"
parsed_date = datetime.strptime(date_string, "%Y-%m-%d %H:%M:%S")
print("Parsed Date:", parsed_date)
In this program, the $ python main.py Parsed Date: 2025-02-15 12:34:56 Date Arithmetic
The following example demonstrates how to perform date arithmetic using the
main.py
from datetime import datetime, timedelta
# Get the current date and time
now = datetime.now()
# Add 5 days to the current date
future_date = now + timedelta(days=5)
# Subtract 2 weeks from the current date
past_date = now - timedelta(weeks=2)
print("Current Date:", now)
print("Future Date (5 days later):", future_date)
print("Past Date (2 weeks earlier):", past_date)
In this program, the $ python main.py Current Date: 2025-02-15 12:34:56.789012 Future Date (5 days later): 2025-02-20 12:34:56.789012 Past Date (2 weeks earlier): 2025-02-01 12:34:56.789012 Working with Time Zones
The following example demonstrates how to work with time zones using the
main.py
from datetime import datetime
import pytz
# Get the current time in UTC
utc_now = datetime.now(pytz.utc)
print("Current Time in UTC:", utc_now)
# Convert UTC time to a specific time zone
eastern = pytz.timezone('US/Eastern')
eastern_time = utc_now.astimezone(eastern)
print("Current Time in US/Eastern:", eastern_time)
In this program, the $ python main.py Current Time in UTC: 2025-02-15 12:34:56.789012+00:00 Current Time in US/Eastern: 2025-02-15 07:34:56.789012-05:00 SourcePython datetime - Documentation
In this article, we have shown how to use the AuthorMy name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming. List all Python tutorials. |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |