nope
nope

Reputation: 197

How to set a a specific time using datetime

I want to set specific time

I've started this

def function():
    current_time = datetime.datetime.time(datetime.datetime.now()).isoformat()[0:8]
    schedule = datetime.time(10, 00, 00)
    if current_time == schedule:

I want check if the current_time is 10 o'clock, is there any better way of doing this?

Upvotes: 0

Views: 101

Answers (2)

Martin Evans
Martin Evans

Reputation: 46759

import datetime

dt = datetime.datetime.now()

if dt.hour == 10 and dt.minute == 0:
    print "it's time"

Upvotes: 1

Daniel Roseman
Daniel Roseman

Reputation: 599470

There's no need for all that complicated code.

current_time = datetime.datetime.now().time()
schedule = datetime.time(10, 00, 00)
current_time == schedule

Although that will only be true on the exact microsecond of 10:00:00, which is almost certainly not what you want. Do you perhaps want to check that it is in the hour of ten o'clock? That would be even simpler:

datetime.datetime.now().hour == 10

Upvotes: 1

Related Questions