3

How do I compare an object datetime.datetime.now().time() with an integer like 12?

I need to make an if condition, which checks whether the time of day is before 12PM or after 12PM and take corresponding action.

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
Sir Muffington
  • 309
  • 1
  • 8
  • 14
  • time objects have attributes which correspond hour, minute, second, microsecond, and timezone. See https://docs.python.org/3/library/datetime.html – R. Arctor Oct 04 '19 at 20:04

6 Answers6

3
d = datetime.datetime.now().time()
if d.hour > 12:
...
JulienV
  • 358
  • 2
  • 9
3

Simple: you don't. You create a datetime object to compare instead:

import datetime
a = datetime.datetime.now().time()
b = datetime.time(12, 00, 00, 000000)
if a < b:
     print("Do x here")
else:
      print("Do y here")
Sir Muffington
  • 309
  • 1
  • 8
  • 14
  • No, it wasn't. I just figured it out by myself thanks to @R. Arctor's comment. And I will select the answer with the most votes (currently it's still a tie). – Sir Muffington Oct 05 '19 at 10:44
  • I see…well, technically, this code does not answer the question because it isn't comparing a `datetime` to an integer. – martineau Oct 05 '19 at 14:39
  • It does: you can't compare an integer with an datetime object. None of the other answers do that too. AFAIK, it's impossible. – Sir Muffington Oct 05 '19 at 15:04
1

This can be easily checked using .strftime("%p"). Just have a look into below example.

Based on your area time may differ but still you can test it for different timezones. Check pytz for that.

If you get

  1. PM, that means, it's after 12 PM inclusive (and before midnight)
  2. AM, that means, it's before 12 PM (and from midnight onwards)

Example code:

import datetime

now = datetime.datetime.now() 
print(now) # 2019-10-04 22:11:46.655252
print(now.strftime("%p")) # PM
print(now.time().strftime("%p")) # PM
hygull
  • 8,464
  • 2
  • 43
  • 52
1

Apart from above mentioned solutions, you could also do this:

import time
d = time.localtime()
if d.tm_hour > 12 && d.tm_sec>0:
   ...

There is a thread that discuss why using time module could be better than datetime.

Anshul Choudhary
  • 225
  • 2
  • 12
1

The class datetime.time() has an hour attribute that you can use for your purpose.

import datetime

datetime_object = datetime.datetime.now().time()

if datetime_object.hour >= 12:
    print("Wow it's after 12 pm")
else:
    print("Wow it's before 12 pm")
Daniel Ocando
  • 3,554
  • 2
  • 11
  • 19
1

You can't compare a datetime instance with an integer directly, but you can convert one to an integer by first using the timestamp() method to convert it to a floating-point value, and then converting that to an integer with the built-in round() function.

In the code below, an integer representing noon on the current day is created, and then that's compared to an integer representing the current date and time.

Since this requires one or more intermediate steps, it would probably be more efficient to just create a datatime object representing 12 pm on the current day and compare that to current date and time (as you do in your own answer).

import datetime

now = datetime.datetime.now()
noon = datetime.datetime(now.year, now.month, now.day, hour=12)

noon_today = round(noon.timestamp())  # Convert to integer.
now_as_int = round(now.timestamp())

if now_as_int < noon_today:
     print("before noon")
else:
     print("noon or later")
martineau
  • 119,623
  • 25
  • 170
  • 301