Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

232
Views
How to get interval between hours and put into list from start into finish?

For example, I will be given a time on hours with type DateTime hours like this

for the starter

  1. my starttime is 00:00
  2. endtime is 02:00

and every time 30 minutes I like to input the value into a List<DateTime> so, how can I get the value to put into a list that is look like this?

  • 00:00
  • 00:30
  • 01:00
  • 01:30
  • 02:00

My Code

        DateTime starTime = new DateTime();
        DateTime endTimes = new DateTime();
        DateTime interval = new DateTime();
        List<DateTime> intervals = new List<DateTime>();
        starTime = DateTime.ParseExact(fulldate + "00:00",
                                "yyyy/MM/dd HH:mm",
                                CultureInfo.InvariantCulture);
        endTimes = DateTime.ParseExact(fulldate + "02:00",
                                "yyyy/MM/dd HH:mm",
                                CultureInfo.InvariantCulture); ;
        interval = starTime;
        for (int i = 0; i < 24; i++)
        {
            interval.AddHours(0.5);
            intervals.Add(interval);
            if (interval.ToString("HH:mm") == endTimes.ToString("HH:mm"))
            {
                break;
            }
        }

Can anyone help me to solve this?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

With some assumption (that end time is on the same day, that your end time is always something that can be devided by 30 mins, ...) this would work.

var start = new TimeSpan(0, 0, 0);
var end = new TimeSpan(2, 0, 0);
var current = start;

List<DateTime> values = new List<DateTime>();
var startDate = DateTime.Now.Date; // editited after @pinkflowydx33's comment 

values.Add(startDate + start);

while (current < end)
{
    current = current.Add(new TimeSpan(0, 30, 0));
    values.Add(startDate + current);
}

foreach (var v in values)
{
    Console.WriteLine(v);
}
over 4 years ago · Santiago Trujillo Report

0

i prepared that type of solution. - It's loop over number, which represent - times of valueToChange - in this specific case between 30 minutes - and add to the startDate - 30 minutes and also saving to list.

  using System;
                        
    public class Program
    {
        public static void Main()
        {
            
            List<DateTime> intervals = new List<DateTime>();
            
            var changeValue = 30;
            
            var startDate = new DateTime(2010, 05, 12, 13, 00, 00);
            var endDate = new DateTime(2010, 05, 12, 14, 00, 00);
    
            var timeIntervals = System.Math.Abs(startDate.Subtract(endDate).TotalMinutes / changeValue);
            
            for (int i = 0; i < timeIntervals; i++)
            {
               startDate.AddMinutes(30);
               intervals.Add(startDate)
            }
    
    
        }
    }

In this case the start and end date are divided by 30 minutes without rest - so if there will be 13:00 and 13:12 - it's doesn't add the value to List - cause the value doesn't > 30.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!