Jump to content

Comparing times

HeaterUp
Go to solution Solved by mathijs727,
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)

 

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 comment
Share on other sites

Link to post
Share on other sites

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 comment
Share on other sites

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 comment
Share on other sites

Link to post
Share on other sites

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: Intel i9-10850K (R9 3900X died 😢 )| MSI Z490 Tomahawk | RTX 2080 (borrowed from work) - MSI GTX 1080 | 64GB 3600MHz CL16 memory | Corsair H100i (NF-F12 fans) | Samsung 970 EVO 512GB | Intel 665p 2TB | Samsung 830 256GB| 3TB HDD | Corsair 450D | Corsair RM550x | MG279Q

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

Console: PlayStation 4 Pro

Link to comment
Share on other sites

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 comment
Share on other sites

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: Intel i9-10850K (R9 3900X died 😢 )| MSI Z490 Tomahawk | RTX 2080 (borrowed from work) - MSI GTX 1080 | 64GB 3600MHz CL16 memory | Corsair H100i (NF-F12 fans) | Samsung 970 EVO 512GB | Intel 665p 2TB | Samsung 830 256GB| 3TB HDD | Corsair 450D | Corsair RM550x | MG279Q

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

Console: PlayStation 4 Pro

Link to comment
Share on other sites

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

×