Nil Pun
Nil Pun

Reputation: 17373

C# Get List of Time 00AM to 23PM on to List

Scenario: I'm trying to populate the C# List with all the time in a day as shown in example below:

00AM to 23PM.

Could someone please help me whether this can be achieved from LINQ or similar?

Thanks.

Upvotes: 0

Views: 1250

Answers (2)

Asif Mushtaq
Asif Mushtaq

Reputation: 13150

DateTime date = new DateTime(); 
var result = Enumerable.Repeat(date, 24)
                       .Select((x, i) => x.AddHours(i).ToString("HH tt"));

Upvotes: 2

RePierre
RePierre

Reputation: 9566

Assuming you want the difference between two succesive values to be one second, you can use this query:

var result = Enumerable.Range(0, 23 * 3600).Select(x => TimeSpan.FromSeconds(x)).ToList();

The number 23 * 3600 stands for total number of seconds between 00:00 and 23:00. If you want to have values from minute to minute, use this:

var result = Enumerable.Range(0, 23 * 60).Select(x => TimeSpan.FromMinutes(x)).ToList();

Upvotes: 4

Related Questions