Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
HeaterUp

Comparing times

6 hours ago, espurritado said:

python has datetime. wit it you can create an object representing a time, a date or a date and time.

you can make it so it parses an input string in whichever format you want to give it to it

 

15 hours ago, HeaterUp said:

So what I am looking to do is compare if 11:15 am is before the time of 10:30 am and different times this same way. I know I could take the first two numbers and compare the hour then compare the next two numbers as the minutes and evaluate the am or pm but is there an easier way to accomplish this? Maybe a python library or anything like that?

In Python (using dateutil library)

from dateutil import parser

if __name__ == "__main__":
	t1 = parser.parse("10:40")
	t2 = parser.parse("11:50")
	print(t2 - t1)

 

Recommended Posts

Posted · Original PosterOP

So what I am looking to do is compare if 11:15 am is before the time of 10:30 am and different times this same way. I know I could take the first two numbers and compare the hour then compare the next two numbers as the minutes and evaluate the am or pm but is there an easier way to accomplish this? Maybe a python library or anything like that?

Link to post
Share on other sites
Posted · Original PosterOP
6 hours ago, M.Yurizaki said:

How is the time presented? If it's a string, then yes, that's all you can do. If it's a binary format, then it's likely split up into hours (in 24 hour time) and minutes

It's read in from a file as a string. I'm not opposed to reading it in as binary if there is a way to do that, but I have a feeling it maybe less lines to stick to the string method.

Link to post
Share on other sites

python has datetime. wit it you can create an object representing a time, a date or a date and time.

you can make it so it parses an input string in whichever format you want to give it to it


The best way to measure the quality of a piece of code is "Oh F*** "s per line

Link to post
Share on other sites
Posted · Best Answer
6 hours ago, espurritado said:

python has datetime. wit it you can create an object representing a time, a date or a date and time.

you can make it so it parses an input string in whichever format you want to give it to it

 

15 hours ago, HeaterUp said:

So what I am looking to do is compare if 11:15 am is before the time of 10:30 am and different times this same way. I know I could take the first two numbers and compare the hour then compare the next two numbers as the minutes and evaluate the am or pm but is there an easier way to accomplish this? Maybe a python library or anything like that?

In Python (using dateutil library)

from dateutil import parser

if __name__ == "__main__":
	t1 = parser.parse("10:40")
	t2 = parser.parse("11:50")
	print(t2 - t1)

 


Desktop: Corsair RM550x | Ryzen R9 3900X |RTX 2080 (borrowed from work) - MSI GTX 1080 | 64GB 3600MHz CL16 memory | Asus X470-F Gaming | Samsung 970 EVO 512GB | Samsung 830 256GB | 3TB HDD | Corsair 450D | Corsair H100i (NF-F12 fans) | MG279Q

Laptop: Surface Pro 7 (i5, 16GB RAM, 256GB SSD)

Console: Playstation 4 Pro

Link to post
Share on other sites
18 hours ago, mathijs727 said:

 

In Python (using dateutil library)


from dateutil import parser

if __name__ == "__main__":
	t1 = parser.parse("10:40")
	t2 = parser.parse("11:50")
	print(t2 - t1)

 

Because it was in OP's list of concerns/requirements, but is that format 24 Hr? Or is there something you can add for AM/PM?

Link to post
Share on other sites
17 hours ago, M.Yurizaki said:

Because it was in OP's list of concerns/requirements, but is that format 24 Hr? Or is there something you can add for AM/PM?

Yes that works too (i.e. 10:00PM gets stored as 22 hours)


Desktop: Corsair RM550x | Ryzen R9 3900X |RTX 2080 (borrowed from work) - MSI GTX 1080 | 64GB 3600MHz CL16 memory | Asus X470-F Gaming | Samsung 970 EVO 512GB | Samsung 830 256GB | 3TB HDD | Corsair 450D | Corsair H100i (NF-F12 fans) | MG279Q

Laptop: Surface Pro 7 (i5, 16GB RAM, 256GB SSD)

Console: Playstation 4 Pro

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


×