Here is the script:
from datetime import datetime, timedelta
today = now = datetime.today()
today = datetime(today.year, today.month, today.day)
print (timedelta(days=7-now.weekday()) today - now)
I need the result to be an int because the program requires the date in int.
CodePudding user response:
Your result is a timedelta.
You can get the days of a timedelta with the days
property:
delta = timedelta(days=7-now.weekday()) today - now)
print(delta.days)
CodePudding user response:
If you mean int as for number of days - use timedelta.days, if you mean seconds - here's a solution for both.
from datetime import datetime, timedelta
today = now = datetime.today()
today = datetime(today.year, today.month, today.day)
td = timedelta(days=7-now.weekday()) today - now
print(td.days)
td_seconds = td.total_seconds()
print(round(td_seconds)) # rounded
CodePudding user response:
A little bit confused on the goal of the script, but I assume you want to find the number of days between the current day and the following Monday.
Assumptions:
- The number of days is calculated until 12am Monday morning. In other words, the days count does not include Monday.
Code to count only the number of full days until Monday:
from datetime import datetime, timedelta
today = datetime.today()
print(int(6 - today.weekday()))
Output (current day is Wednesday 1:29AM):
4
Code to calculate the number of days including partial days (i.e. current day 1:29 on Wednesday, there is about 4.91 days until 12am on Monday).
Detailed view:
from datetime import datetime, timedelta
# Get current date
today = datetime.today()
# Calculate number of days until Monday (partial days count as full day)
days_until_monday = timedelta(days=int(6 - today.weekday()) 1)
# Get date of next Monday
next_monday = today days_until_monday
# Format dates
today = datetime(today.year, today.month, today.day, today.hour, today.minute, today.second)
next_monday = datetime(next_monday.year, next_monday.month, next_monday.day, 0, 0, 0)
# Difference up to the nearest second
delta = next_monday - today
delta = delta.total_seconds()
# Convert to days
delta = delta / 86400
# Print result
print(float(delta))
Output:
4.912685185185185
Hope this helps :)