tygzy
tygzy

Reputation: 716

Python datetime get only days from datetime object

I am making a website and I want to compare the dates but then when I do that it gives me an extra 0:00:00 which I don't want this is my code:

% if (datetime.datetime.strptime(row['due_date'], "%Y-%m-%d") - cur_date).days <= 0:
      <kbd style="background-color: #a52c2c;">{{datetime.datetime.strptime(row['due_date'], "%Y-%m-%d").date() - cur_date.date()}}</kbd>

% elif (datetime.datetime.strptime(row['due_date'], "%Y-%m-%d") - cur_date).days <= 2:
      <kbd style="background-color: #cc781e;">{{datetime.datetime.strptime(row['due_date'], "%Y-%m-%d").date() - cur_date.date()}}</kbd>

% else:
      <kbd>{{datetime.datetime.strptime(row['due_date'], "%Y-%m-%d").date() - cur_date.date()}}</kbd>
% end

I know it is messy but it works and it returns this: 3 days, 0:00:00 but I don't want the extra minutes etc. I know this might already be asked but I haven't seen anything

Upvotes: 1

Views: 16371

Answers (2)

HGalloway
HGalloway

Reputation: 31

from datetime import date

CurrentDay = date.today().day
print(CurrentDay)

CurrentDay is what you want. It just prints the day.

Upvotes: 3

Mehran Jalili
Mehran Jalili

Reputation: 705

This is a nice example for date comparison.

import datetime


str_date = "2019-03-18"

print(datetime.datetime.today().date())

object_date = datetime.datetime.strptime(str_date, '%Y-%m-%d')
if datetime.datetime.today().date() >= object_date.date():
    print(True)
else:
    print(False)

print((object_date.date() - datetime.datetime.today().date()).days)

Upvotes: 4

Related Questions