0

I am trying to write a script that runs continuously in the background to countdown to a repeated weekly event. For example, it should tell me how many days, hours, and minutes it will take to reach the specified time.

I know how to do it if I had a specific date and time.

import datetime
delta = datetime.datetime(2018, 5, 5, 8) - datetime.datetime.now()

But what if I don't have a specific date and time? Can datetime let me choose the day of the week?

EDIT:

i.e. Some pseudocode like this is what I need.

delta = datetime(Thursday 8 PM) - datetime.datetime.now()
#returns datetime or timedelta in days, hours, minutes
johntan05
  • 105
  • 1
  • 2
  • 9
  • try looking up this question, this should help you: [Day of Week](https://stackoverflow.com/questions/9847213/how-do-i-get-the-day-of-week-given-a-date-in-python) – Stonecutter Sep 03 '18 at 09:12
  • I have been looking it up and I've seen that link. I don't need to know the day of the week depending on what datetime I have, I'm asking if there's a datetime weekday attribute I can specify within datetime.datetime. Something like this pseudocode: `delta = datetime(Thursday 8 PM) - datetime.datetime.now()` – johntan05 Sep 03 '18 at 09:18
  • I don't really understand this. Are you trying to pick a day and time at random? Are you counting down to the selected time this week, next week, next month, next year? Are you trying to allow users to set a time that it counts down to each week? I think a little explanation of the end goal might help with contextualising this. – Ethan Field Sep 03 '18 at 09:26
  • @EthanField Thanks, I didn't realize I was being unclear. I'll add more context in the original post. – johntan05 Sep 03 '18 at 09:32
  • You'll still need to specify a time for this event somehow. How exactly would you like it to work if we're declaring a "day" as the event time? The event is triggered at precisely 12:00:00 on the day that the event should trigger, or will it be a specified time and day each week? – Ethan Field Sep 03 '18 at 09:52

1 Answers1

2

EDIT: Thanks Ethan, i appreciate your constructive advice. I wrote a small script which should do what you want:

import datetime
import time
wanted_day = 'thursday'
wanted_time = 8

list = [['monday', 0],['tuesday', 1],['wednesday', 2],['thursday', 3],['friday', 4],['saturday', 5],['sunday', 6]]

for i in list:
    if wanted_day == i[0]:
        number_wanted_day = i[1]

# today delivers the actual day
today = datetime.datetime.today().weekday()

# delta_days describes how many days are left until the wanted day
delta_days = number_wanted_day - today

# time delivers the actual time
time = time.localtime(time.time())

if wanted_time > time[3]:
    delta_hours = wanted_time - time[3]
    delta_mins = 59 - time[4]
    delta_secs = 59 - time[5]

else:
    delta_days = delta_days - 1
    delta_hours = 23 - time[3] + wanted_time
    delta_mins = 59 - time[4]
    delta_secs = 59 - time[5]

print [delta_days, delta_hours, delta_mins, delta_secs]

The output looks like this then:

[2, 21, 3, 49]

2 is the number of days, 21 the number of hours, 3 the number of mins and 49 the number of secs (I used thursday 8 am as wanted time). You just need to input the time in the format 0-23, with am and pm you would need to adapt it a bit

Stonecutter
  • 131
  • 5
  • I think you could improve this answer by providing a functional example rather than just presenting two snippets and stating that the combination of the two will give the desired result. – Ethan Field Sep 03 '18 at 09:30
  • THANK YOU SO MUCH!! couldn't get the logic quite in my head until I saw your code. – johntan05 Sep 04 '18 at 03:58
  • You're welcome! If my answer helped you, you can please mark it as the "right" answer, which will increase my reputation a bit, which lets me comment and so on :) – Stonecutter Sep 04 '18 at 08:11