# Online Python compiler (interpreter) to run Python online.
# Write Python 3 code in this online editor and run it.
from datetime import datetime
# Get current date and time
now = datetime.now()
# 1. Current day, month, year, hour, minute, timestamp
print("Current Date and Time Details:")
print("Day:", now.day)
print("Month:", now.month)
print("Year:", now.year)
print("Hour:", now.hour)
print("Minute:", now.minute)
print("Timestamp:", now.timestamp()) # Seconds since Jan 1, 1970
# 2. Format the current date
formatted_date = now.strftime("%m/%d/%Y, %H:%M:%S")
print("\nFormatted Date:", formatted_date)
# 3. Convert string "5 December, 2019" to datetime
time_str = "5 December, 2019"
date_obj = datetime.strptime(time_str, "%d %B, %Y")
print("\nConverted Datetime Object from String:", date_obj)
# 4. Time difference between now and New Year
new_year = datetime(now.year + 1, 1, 1)
time_left = new_year - now
print("\nTime left until New Year:", time_left)
# 5. Time difference between 1 Jan 1970 and now
epoch = datetime(1970, 1, 1)
time_since_epoch = now - epoch
print("Time since Jan 1, 1970:", time_since_epoch)
# 6. Uses of datetime module
print("\nCommon Uses of datetime Module:")
print("- Time series analysis (e.g., stock market, weather data)")
print("- Logging timestamps for activities")
print("- Scheduling blog or social media posts")
print("- Date calculations (expiry, age)")
print("- Measuring execution time")
print("- Handling time zones and daylight saving")