Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
ex: I have 00:00:00 and 23:59:59
I want create a list be like with less code:
[00:00:00,
00:00:01,
00:00:02,
00:00:03,
...
00:01:00,
00:01:01,
00:01:02,
...
23:59:58,
23:59:59]

Thanks so much!

What I have tried:

I don't have any idea, please help me.
Posted
Updated 18-Mar-22 4:31am

Check the following link to generate a list of time with range.
.net - C# Is there a way to make a list of time range? Configurable - Stack Overflow[^]
 
Share this answer
 
Comments
Maciej Los 18-Mar-22 10:32am    
Good one. I'm affraid that the code provided under link is too hard to understand to OP.
Steps to do:
1) Declare a list of DateTime:
2) Declare start time and end time
3) Declare counter
4) define while loop
4a) Add second in each iteration of loop till start time is less then end time
4b) increase counter
5) display dates in time format

C#
//1
List<DateTime> myList = new List<DateTime>();
//2
DateTime starttime = DateTime.Today;
DateTime endtime = starttime.AddDays(1).AddSeconds(-1);
//3
int i  = 0;
//4
while(starttime<endtime)
{
	starttime = starttime.AddSeconds(i);
	myList.Add(starttime);
	i++;
}
//5
foreach(DateTime tm in myList)
	Console.WriteLine(tm.ToString("HH:mm:ss"));



That's all!

For further details, please see:
DateTime Struct (System) | Microsoft Docs[^]
DateTime.AddSeconds(Double) Method (System) | Microsoft Docs[^]
Standard date and time format strings | Microsoft Docs[^]
Custom date and time format strings | Microsoft Docs[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900