0

For some reason, tablesorter works properly only on strings that represents time lower than '25:00'. It's putting anything above '25:00' lower than '24:12' or '09:24' for example. So I probably need to convert the string to a time format. To be able to use col-index: { sorter: 'time'} in the tablesorter.

This is how it looks now

This is how it looks now

I've read datetime and time docs, found some great answers on SO, but couldn't find exactly what I need.

The closest I get is:

from datetime import datetime

datetime_object = datetime.strptime('12:55', '%M:%S')

print(datetime_object)

>>> 1900-01-01 00:12:55
6
  • 1
    Can you not simply split it, by :, and then split[0] * 60 + split[1]? If it's not a datetime then don't try to make it a datetime. Feb 8, 2019 at 7:20
  • If I understand correctly you want to sort on the time? Then you can sort on the unix time of each event? dateEvent = datetime.datetime.strptime(timeEvent, "%M:%S") ,unix = time.mktime(dateEvent.timetuple())
    – Kspr
    Feb 8, 2019 at 8:01
  • @Kspr I want to sort a time that I am getting from an NHL API. This is a hockey players time on ice per game. Feb 8, 2019 at 8:03
  • How about using re to split the string at the : symbol and store in hour and second variable?
    – Kspr
    Feb 8, 2019 at 8:05
  • @Archer you mean, I need to do this in JS, not in Python, right? Feb 8, 2019 at 8:13

2 Answers 2

1

If you're using my fork of tablesorter, there is an additional countdown parser. Load the parser-duration.js file and set the sorter to "countdown".

-1

time() method is used to get only time from the given datetime object.

from datetime import datetime

datetime_object = datetime.strptime('12:55', '%M:%S').time()
print(str(datetime_object)[3:])

output:

12:55

5
  • you can do it with slicing (see edited code), if answer helps you then click on accept.
    – Eternal
    Feb 8, 2019 at 7:57
  • it will be a string again, so there is no point. You couldn't slice the datetime object, I believe. Feb 8, 2019 at 8:01
  • I don't think you can change the representation of object itself however you can extract hour minute and second as integers.
    – Eternal
    Feb 8, 2019 at 8:04
  • From a datetime object you can try using datetime_object.hour
    – Kspr
    Feb 8, 2019 at 8:12
  • But it will return an integer representing hour, minute or second what he wants is representing datetime object with only minute two values, which i guess is not possible. @Kspr
    – Eternal
    Feb 8, 2019 at 8:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.