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

236
Views
¿Cómo obtener el intervalo entre horas y ponerlo en la lista de principio a fin?

Por ejemplo, se me dará un tiempo en horas con tipo DateTime horas como este

para el motor de arranque

  1. mi hora de inicio es 00:00
  2. la hora de finalización es a las 02:00

y cada 30 minutos me gusta ingresar el valor en List<DateTime> entonces, ¿cómo puedo obtener el valor para ponerlo en una lista que se vea así?

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

Mi código

 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; } }

Alguien puede ayudarme a resolver esto?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Con alguna suposición (que la hora de finalización es el mismo día, que su hora de finalización siempre es algo que se puede dividir en 30 minutos, ...) esto funcionaría.

 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

Preparé ese tipo de solución. - Es un bucle sobre el número, que representa - tiempos de valueToChange - en este caso específico entre 30 minutos - y se agrega a la fecha de inicio - 30 minutos y también se guarda en la lista.

 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) } } }

En este caso, la fecha de inicio y finalización se dividen por 30 minutos sin descanso, por lo que si serán las 13:00 y las 13:12, no agrega el valor a la Lista, porque el valor no es > 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!