Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Comparing dates
#5
(May-03-2020, 01:24 AM)snippsat Wrote: When you shall compare dates you need eg string str(row[2] to be a datetime object.
Here i quick demo,and i use Pendulum because it's great and build on top of datetime,just doing stuff better.
>>> import pendulum
>>> 
>>> s = 'Beltway, 2020-04-30 23:18:14'
>>> my_date = s.split(', ')[1]
>>> my_date
'2020-04-30 23:18:14'
>>> type(my_date)
<class 'str'>

# Can parse a string automatically   
>>> my_date = pendulum.parse(my_date)
>>> my_date
DateTime(2020, 4, 30, 23, 18, 14, tzinfo=Timezone('UTC'))
>>> type(my_date)
<class 'pendulum.datetime.DateTime'>

# Now that is a datetime object can compare,eg just use yesterday as a example
>>> yesterday = pendulum.yesterday()
>>> yesterday
DateTime(2020, 5, 2, 0, 0, 0, tzinfo=Timezone('Europe/Berlin'))
>>> 
>>> my_date < yesterday
True
>>> my_date == yesterday
False
>>> my_date <= yesterday
True
>>> my_date >= yesterday
False

Thank you so I see what can be say a hard code date, will I need to manually update or change this daily or is it possible to say like today minus 1 to equal the output or am I looking/thinking and making this way more complicated? Sorry this is all new to me and first time comparing a date that’s in the past hope what I’m trying to do makes sense
Reply


Messages In This Thread
Comparing dates - by tpolim008 - May-01-2020, 02:26 PM
RE: Comparing dates - by Larz60+ - May-01-2020, 05:37 PM
RE: Comparing dates - by tpolim008 - May-02-2020, 04:32 PM
RE: Comparing dates - by snippsat - May-03-2020, 01:24 AM
RE: Comparing dates - by tpolim008 - May-03-2020, 01:32 AM
RE: Comparing dates - by snippsat - May-03-2020, 01:52 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020