How to create a list of strings representing 15 minute time intervals between start and end time in one day using C #?

I am creating a form in which the user can specify the start and end times using the given values. I would like to generate a list of string representations of the available 15 minute intervals between 9 AM and 5 PM in one day.

+3
source share
5 answers
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)));

Then for the current day, if you save the values ​​in the database.

+8
source

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

+7
source

        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);
        }
+5

- ;

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);
}
+3
source

Create an outer loop with a start date. Let this cycle use AddDays to add a day to it before the end date.

Create a loop inside this loop and start at 9AM and loop up to 5PM in 15 minute increments, using Silky to specify the DateTime.AddMinutes method. In 15 minute increments, you can add Time to the list of strings.

:)

0
source

Source: https://habr.com/ru/post/1715382/


All Articles