Link to home
Start Free TrialLog in
Avatar of Nugs
Nugs

asked on

Find position in array?

I have a question... Not really sure this is the way to do it, so i am open to any other suggestions..

I want to split a 24 hour day up into 2 hour periods... I want to then look at the current hour and get the period this hour fits into.

So i built an array:

int[] HourPeriods = new int[12] { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24 };

And can get the current hour, and loop through the hour periods...

        foreach (int hr in HourPeriods)
        {
            //DateTime.Now.Hour;
        }

But how to i do this? Say if the hour is 15 how do i get the 16 period from the array???

Nugs
Avatar of Joel Coehoorn
Joel Coehoorn
Flag of United States of America image

I think you're making it harder than it has to be:
int PeriodIndex = DateTime.Now.Hour / 2;
SOLUTION
Avatar of Solar_Flare
Solar_Flare

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Nugs
Nugs

ASKER

jcoehoorn: I think you are right that i am making it more complex than it need be. Unfortunatley your example is way off from what i am looking for.

Solar_Flare: Just by looking at your example i could see it would work. But after much consideration and jcoehoorn comment i decided it was to complicated for what needed to be done...

What do you guys think of the attached code...

Since it is the hours i am looking at these number will always be between 1 and 24... Say i run the code now at 3pm (15 hr) this will increment it to 16 hundred hour...

Nugs

        int HourPeriodIndex = 1;
        if (DateTime.Now.Hour % 2 != 0)
        {
            HourPeriodIndex = DateTime.Now.Hour + 1;
        }
        else
        {
            HourPeriodIndex = DateTime.Now.Hour;
        }

Open in new window

Try this:
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Nugs

ASKER

Thanks everyone. I eventually opted for jcoehoorn simpler solution here, but none of the solutions posted were wrong by any means so i split the points... Thanks for all the input.