Hello all again, hopefully the last bit I'm stuck on.

So at the moment, I'm wanting a piece of code that recognises any time between say 9am and 11am, 11am and 1pm, 1pm and 3pm, and so on (To create a pie chart)

It doesn't have to recognise time, it could just be strings or integers, although that is probably the easiest way, what I have at the moment is this;

I understand that it is probably pretty wrong, although hopefully you get the idea of what I'm trying to do.

If OrderTimeGrid1.Cells[0,0] = '13:00:00' <> '15:00:00' then
    TimeCount1 := TimeCount1 + 1;
OrderTimeGrid2.Cells[0,0] := TimeCount;

The OrderTimeGrid1 Cells simply have something like 14:32:12 in them, without a variable. So just a string, might need converting etc

Any help appreciated.

Recommended Answers

All 3 Replies

Depending on what data type you use, just write a function that does this for you. If you want a little more advanced code, you can use operator overloading, so you could use any type you want for the time part. The basic function declaration (with strings) could look something like this:

function IsInRange(const AValue, ALow, AHigh: string): Boolean;

Thank you for the reply,

So what would be the best way of going about it with a range of times like my first post? Rather than just a few strings.

Also, sorry to keep on troubling but how is it possible to find an average of times? As obviously adding times together then dividing doesn't work at all. Such as 00:15:25 and 00:20:25, I want to find the average time (Which would be around 00:17:55 I'd expect)

For this, I suppose it would be easier to take the time and convert it into an integer of seconds, find the average and then convert back to time, however not too sure how to convert from time to integer. Thanks!

  1. Can you explain what you you mean by the "range of times"? Is the function not what you need, or do you need help with the implementation?

  2. For averaging, I'd make a function that takes your time strings and converts them to three integers (hour, minute and second) and does the calculating with those. After, you can convert it back to a string. You need to use Copy() on the three parts, and then you can use StrToInt() to convert to integer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.