1 Answer
- Newest
- Most votes
- Most comments
1
Just subtract an hour from the current time with timedelta(hours=1)
and format it like your file names using strftime("%Y%m%d-%H%M%S")
.
You will have something like:
from datetime import datetime, timedelta import pytz desired_timezone = pytz.timezone('America/New_York') # Replace 'Your_Time_Zone' with your actual time zone current_datetime = datetime.now(desired_timezone) one_hour_ago_datetime = current_datetime - timedelta(hours=1) formatted_current_datetime = current_datetime.strftime("%Y%m%d-%H%M%S") formatted_one_hour_ago_datetime = one_hour_ago_datetime.strftime("%Y%m%d-%H%M%S") print("Current time:", formatted_current_datetime) print("One hour ago:", formatted_one_hour_ago_datetime)
Resources:
Relevant content
- Accepted Answerasked 4 days ago
- asked 9 months ago
- AWS OFFICIALUpdated 3 years ago
- AWS OFFICIALUpdated 4 months ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 5 months ago
thanks a lot. The way you added the TIMEDELTA made the difference. Your solution worked for me :)