Having derived this logic to bind dropdownlist to set of time intervals, I would like to get this improved in terms
-
Use native data types designed to date and time
-
Make it configurable
pseudocode
for (var hoursCount = 0; hoursCount <= 12; i++) {
for (var timeSlots = 0; timeSlots < 2; j++) {
string hourAndMinute = hoursCount;
if (timeSlots == 0) {
hourAndMinute += ":00 AM";
} else {
hourAndMinute += ":30 AM";
}
if (hourAndMinute != "12:00 AM" || hourAndMinute != "12:30 AM") {
alert(hourAndMinute);
}
}
}
Output
0:00 AM
0:30 AM
.
.
.
12:30 AM
It’s unclear what sort of “configuration” you want here, but as you’re only dealing with a time, I would personally use Noda Time which has the
LocalTimestruct for handling this sort of thing. (Disclaimer: I’m the main developer on Noda Time, so I’m somewhat biased.) You could just useDateTime, but as you don’t want a date…It’s not really clear what kind of “configuration” you want, but in Noda Time you could write:
With more details of what you want to do, we can no doubt help more.
EDIT: Okay, it sounds like maybe you want something like:
Then call it with:
(Or whatever…)
Alternative using
DateTime: