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.
views:
199answers:
5+1 for offering a simple explanation without feeding the answer.
ph0enix
2009-08-18 13:51:08
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
2009-08-18 02:03:03
+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
2009-08-18 02:12:00
+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
2009-08-18 02:12:43
Cool, but I wouldn't call that clear code, especially that magic number 33 on the Range()
Vinko Vrsalovic
2009-08-18 09:09:21
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
2009-08-18 17:11:38
@rp: They are numbers, but not at all magic! Just read the constructor signature and it'll all be clear.
Vinko Vrsalovic
2009-08-18 17:28:35
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
2009-08-18 21:30:40
@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
2009-08-18 23:42:49
33 Could be written out as 8 * 4 + 1, which is how it is calculated: 8 hours * 4 per hour + 1 overlapping
Yuriy Faktorovich
2009-08-19 00:12:20
@Shurik: It will eventually, luckily you'll be dead by then. Your name reminded me of the Russian movies, thanks.
Yuriy Faktorovich
2009-08-19 04:05:31
+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
2009-08-18 02:13:42
Then I tried to use it in an ASP.Net application and got an out of memory error.
Shurik
2009-08-18 17:10:14
Because it wasn't that clear then :-) You probably messed up the loop and made it to never terminate.
Vinko Vrsalovic
2009-08-18 17:27:32