Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a List of hours

I'm trying to create list of hours my code like

 var hours = Enumerable.Range(00, 24).Select(i => i.ToString("D2")));

it generates something like this in 24 hour format but actually i need same thing not on 24 hours but 12 like after with AM PM postfix

what is the best way to generate hour list with 00.00 AM/PM formatted

Thanks

like image 321
Gayan Avatar asked Dec 04 '25 13:12

Gayan


2 Answers

You can create a DateTime instance then use ToString format of hh.mm tt.

var hours = Enumerable.Range(00, 24)
    .Select(i => new DateTime(2000,1,1,i,0,0).ToString("hh.mm tt"));

You need to execute the query using something like ToArray() to get the result since the behavior of linq is deferred execution.

like image 184
Yuliam Chandra Avatar answered Dec 10 '25 06:12

Yuliam Chandra


var hours = Enumerable.Range(00, 24).Select(i => (DateTime.MinValue.AddHours(i)).ToString("hh.mm tt"));
like image 42
jmcilhinney Avatar answered Dec 10 '25 06:12

jmcilhinney



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!