Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

239
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda