1

I cannot understand how to use information on the current datetime in the code at the bottom. I calculated the current datetime as

import datetime

now = datetime.now()
reftime1 =  datetime(now.year,now.month,now.day,now.hour) # only NaN values

vs the example in the code below

reftime2 = datetime.datetime(2021,4,10,20) # no error 

The code (From string to datetime, looking at the current (local) time) where I would like to replace reftime1 is

import datetime
import dateparser 

def timedelta2str(sec):
    hours, remainder = divmod(sec, 3600)
    return f'{int(hours):02}:{int(remainder//60):02}'

def formatTimeAgo(string, reftime):
    dtobj = dateparser.parse(string, settings={'RELATIVE_BASE': reftime})
    if isinstance(dtobj, datetime.datetime):
        td = reftime - dtobj
        if td >= datetime.timedelta(1):
            return timedelta2str(td.total_seconds())
        else:
            return (reftime-td).strftime('%H:%M')
    else:
        return "N/A"

# exemplary input
t = ("10 hours", "6 hours", "2 days", "1 day")
reftime1 = datetime.datetime(2021,4,10,20)     # here
for elem in t:
    print(elem, '->', formatTimeAgo(elem, reftime1))

I would need an explanation on what is wrong in my replacement.

LdM
  • 674
  • 7
  • 23
  • 1
    Are you sure that you're importing the actual datetime module? `datetime.now()` should give an `AttributeError` with just an import of `datetime` since the method is `datetime.datetime.now()`. – duckboycool Apr 11 '21 at 03:47
  • I tried with both, so probably I did not got the error only because I was using the right one in my code, but reporting in the question the wrong one. Thank you so much for letting me noticed it. – LdM Apr 11 '21 at 16:24

2 Answers2

1

To get the current day, which I'm assuming is what you're trying to do, you can use:

reftime1=datetime.datetime.now()

implemented:

import datetime
import dateparser 

def timedelta2str(sec):
    hours, remainder = divmod(sec, 3600)
    return f'{int(hours):02}:{int(remainder//60):02}'

def formatTimeAgo(string, reftime):
    dtobj = dateparser.parse(string, settings={'RELATIVE_BASE': reftime})
    if isinstance(dtobj, datetime.datetime):
        td = reftime - dtobj
        if td >= datetime.timedelta(1):
            return timedelta2str(td.total_seconds())
        else:
            return (reftime-td).strftime('%H:%M')
    else:
        return "N/A"

# exemplary input
t = ("10 hours", "6 hours", "2 days", "1 day")
reftime1 = datetime.datetime.now()     # MADE THE CHANGES HERE
for elem in t:
    print(elem, '->', formatTimeAgo(elem, reftime1))

IF, however, you did want to use the 'now.hour' etc format that you were trying to use, then you can use something like:

from datetime import datetime
now = datetime.now()
print(now.hour) # you can change the hour to minute or second; ETC.
StevenHarvey
  • 126
  • 5
0

Change

import datetime

To

from datetime import datetime

will make your first code pieces work

from datetime import datetime

now = datetime.now()
reftime1 =  datetime(now.year,now.month,now.day,now.hour)
arrickx
  • 69
  • 1
  • 6
  • Thank you for your answer, arrickx. Unfortunately, your proposed change does not work in my case. With my dataset I am getting only NaN values, trying to change the for loop as follows: `try: print(elem, '->', formatTimeAgo(elem, ref_time)) except: print("NaN")` . This happens when I use `now.year` ,... so I guess something is still not working – LdM Apr 11 '21 at 16:20