1

I am currently stuck right now. I have moved to C# but one of the scripts in my PHP won't work when i convert it to C#. I have some problems with the array of minutes. Here is my PHP Script:

<?php
$start_hour = 10;
$end_hour = 22;
$minutes_array = array("15", "30", "45");
for($i=$start_hour; $i<($end_hour + 1); $i++){
    $string = $i . ':00';
    echo '<option value="' . $string . '">' . $string . '</option>';
    if($i != $end_hour){
        for($j=0; $j<sizeof($minutes_array); $j++){
            $string = $i . ':' . $minutes_array[$j];
             echo '<option value="' . $string . '">' . $string . '</option>';
        }
    }
}
?>

What it does is it outputs list items from 10 to 22 with 15, 30 and 45 between every count. So it looks like this

DropdownList
10:00
10:15
10:30
10:45
11:00
11:15
etc..

And here is my C# code so far:

int Opened = 8;
    int Closed = 22;

    for (int i = Opened; i < (Closed); i++)
    {
        string String = i + ":00";
        Response.Write(String);
        if (i != Closed)
        {
            for(int j = 0; j<sizeof(); j++)
            {
                String = i + ":" + 
            }
        }
    }

Can anyone help me converting this to C#? It would really make my day!

Thanks in advance, Jens

4 Answers 4

1
var startHour = new DateTime(2000, 01, 01, 08, 00, 00);
var endHour = new DateTime(2000, 01, 01, 22, 00, 00);
var step = TimeSpan.FromMinutes(15);
for (var time = startHour; time <= endHour; time += step)
{
    Console.WriteLine(time.ToString("HH:mm"));
}
Sign up to request clarification or add additional context in comments.

Comments

1

in php you are doing the following code for($i=$start_hour; $i<($end_hour + 1); $i++){ While in c# you are not adding the value. for (int i = Opened; i < (Closed); i++)

Comments

1

using datetime variables

        var open = 8;
        var close = 22;

        var c = new DateTime(2014, 1, 1, open, 0, 0);
        var d = new DateTime(2014, 1, 1, close, 0, 0); ;

        while (c < d)
        {
            Console.WriteLine(string.Format("{0}:{1}", c.ToString("hh"), c.ToString("mm")));
            c = c.AddMinutes(15);
        }

Comments

1

The below code will do what you need:

List<string> Time = new List<string>();

int hourStart = 8;
int hourFinish = 22;

for (int h = hourStart; h <= hourFinish; h++)
{
    for (int m = 0; m < 60; m = m + 15)
    {
        string hours;
        string minutes;

        // this section is used to format the 0 to 00
        if (h == 0)
            hours = "00";
        else
            hours = h.ToString();

        if (m == 0)
            minutes = "00";
        else
            minutes = m.ToString();

        string time = hours + ":" + minutes;

        Time.Add(time);
    }
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.