tags:

views:

199

answers:

5

I'm creating a form where a user will be able to specify a beginning and end times using preset values. I would like to generate a list of string representations of available 15 min intervals between 9 AM and 5 PM in a single day.

+6  A: 

DateTime has a .AddMinutes method. Start there. (DateTime.Now)

Noon Silk
+1 for offering a simple explanation without feeding the answer.
ph0enix
A: 

Create an outer loop that has the start date given. Have this loop use the AddDays to add a day to it until the end date.

Create a loop inside this loop and start with a time of 9AM and loop until 5PM with 15 minute increments using Silky's mentioning of the DateTime .AddMinutes Method. At the 15 minute increments you can add to a list of strings the Time.

:)

klabranche
+2  A: 

Something like the following should do the trick;

for (var time = new DateTime(2000,1,1,9,0,0); time <= new DateTime(2000,1,1,17,0,0); time = time.AddMinutes(15))
{
  Console.WriteLine("{0:t}", time);
}
JohannesH
+3  A: 
List<string> query = Enumerable.Range(0, 33).Select(i => 
    DateTime.Today.AddHours(9).AddMinutes(i * 15).ToString()).ToList();

or

int i = -1;
while(DateTime.Today.AddHours(9).AddMinutes(i * 15).Hour < 17)
    Console.WriteLine(DateTime.Today.AddHours(9).AddMinutes(15 * (++i)));

Thats for the current day, in case you are saving the values in a database.

Yuriy Faktorovich
Very cool, isn't it! The more you use LINQ, the less you use foreach.
rp
I use it all the time, .ToList().ForEach
Yuriy Faktorovich
Cool, but I wouldn't call that clear code, especially that magic number 33 on the Range()
Vinko Vrsalovic
Vinko--Your answer is riddled with magic numbers!
rp
While the last example took a little bit to grasp, it didn't cause an out of memory error when used in ASP.Net, so I'm using it for now.
Shurik
Oh, and doesn't it remind you of Lisp?
Shurik
@rp: They are numbers, but not at all magic! Just read the constructor signature and it'll all be clear.
Vinko Vrsalovic
As magic tricks are clear to Penn and Teller, magic numbers are clear _when_ you understand the signatures involved. Why worry about magic numbers at all if you assume signature familiarity! (Please don't take my tone as snarky, I'm just providing another point of view.)
rp
Shurik--what answer caused a memory problem?
rp
Linq Rocks!!! :)
klabranche
@rp: 33 is not clear from any signature, you need to actually calculate that there are 33 intervals of 15 minutes between 9 and 17, also, if you change that magic number the solution won't work. OTOH, 1900-1-1 is clear from the method signature, and was put there for lack of precision of whether current date is needed or not. You can change it and the solution will still work. I think you are being obtuse just for the sake of providing a different point of view.
Vinko Vrsalovic
33 Could be written out as 8 * 4 + 1, which is how it is calculated: 8 hours * 4 per hour + 1 overlapping
Yuriy Faktorovich
@Shurik: It will eventually, luckily you'll be dead by then. Your name reminded me of the Russian movies, thanks.
Yuriy Faktorovich
+3  A: 

Here's the code

        DateTime start = new DateTime(1900, 1, 1, 9, 0, 0);
        DateTime end = new DateTime(1900, 1, 1, 17, 0, 0);
        DateTime current = start;
        while (current <= end)
        {
            Console.WriteLine(current.ToString("HH:mm"));
            current = current.AddMinutes(15);
        }
Vinko Vrsalovic
I chose this answer for it's clarity.
Shurik
Then I tried to use it in an ASP.Net application and got an out of memory error.
Shurik
Because it wasn't that clear then :-) You probably messed up the loop and made it to never terminate.
Vinko Vrsalovic