Python Date and Time
What can use Date and Time in Python?
Working with dates and times in Python includes the usage of the datetime module, which offers instructions for manipulating dates and times.
Date and Time Basics:
The datetime module affords lessons for operating with dates and times, and the date, time, datetime, timedelta, and timezone training are specifically beneficial.
from datetime import datetime
# Get the current date and time
current_datetime = datetime.now()
print("Current Date and Time:", current_datetime)
# Access individual components
year = current_datetime.year
month = current_datetime.month
day = current_datetime.day
hour = current_datetime.hour
minute = current_datetime.minute
second = current_datetime.second
print(f"{year}-{month}-{day} {hour}:{minute}:{second}")
Formatting and Parsing:
# Formatting a datetime object
formatted_date = current_datetime.strftime("%Y-%m-%d %H:%M:%S")
print("Formatted Date:", formatted_date)
# Parsing a string to a datetime object
date_string = "2023-01-15 14:30:00"
parsed_date = datetime.strptime(date_string, "%Y-%m-%d %H:%M:%S")
print("Parsed Date:", parsed_date)
Timedelta:
The timedelta beauty is a part of the datetime module and is used to symbolize the length or the difference amongst dates or instances.
It permits you to carry out mathematics operations on datetime objects.
from datetime import timedelta
# Calculate the difference between two dates
future_date = current_datetime + timedelta(days=10, hours=5)
time_difference = future_date - current_datetime
print("Future Date:", future_date)
print("Time Difference:", time_difference)
Working with Timezones:
unittest Module:
from datetime import timezone
# Get the current UTC time
utc_time = datetime.now(timezone.utc)
print("UTC Time:", utc_time)
# Convert UTC time to a specific timezone
import pytz
desired_timezone = pytz.timezone('America/New_York')
eastern_time = utc_time.astimezone(desired_timezone)
print("Eastern Time:", eastern_time)
Calendar Module:
import calendar
# Print the calendar for a specific month
cal = calendar.month(2023, 1)
print("January 2023 Calendar:")
print(cal)
Working with Dates:
from datetime import date
# Create a date object
my_date = date(2023, 1, 15)
print("My Date:", my_date)
# Calculate the difference between two dates
days_difference = my_date - date(2023, 1, 1)
print("Days Difference:", days_difference.days)
Timestamps:
A timestamp commonly refers to a illustration of a specific factor in time. It's frequently used to record whilst a particular occasion took place.
timestamp = current_datetime.timestamp()
print("Timestamp:", timestamp)
# Convert timestamp back to datetime
new_datetime = datetime.fromtimestamp(timestamp)
print("Converted Datetime:", new_datetime)
Working with dates and times is a common requirement in severa applications.
The datetime module in Python provides a whole set of equipment for handling those situations, which includes formatting, parsing, arithmetic operations, and timezone adjustments.