Monday, 10 August 2009

How to make Combobox with hours of day (quarters)

This is an algorithm which produces the hours of day for every quarter (00:00, 00:15, 00:30, 00:45) and inserts them in a combobox, I used this in my ToDo application which I will soon post and I thought it might be useful to someone.

/*ActionScript*/

var model:Array = [];


var entry:Number = 0;
var entryHour:Number = 0;
var entryMinutes:Number = 0;
var result:String;

//The main loop runs 4*24 times cause we want all hours from 00:00 - 24:00 to be set
//
for (var j:int = 0; j<4*24; j++) {

if (j==0)
{

model.push(result=(entryHour=entryHour).toString()+"0"+":"+"00");
}
else if (entry<3)
{
if(entryHour<10)
{

//if time < 10 adds a 0 so it will look like 09:00 and not 9:00
model.push(result = "0"+(entryHour).toString()+":"+(entryMinutes=entryMinutes+15).toString());
}
else
{
model.push(
result = (entryHour).toString()+":"+(entryMinutes=entryMinutes+15).toString());
}

entry++;

}
else
{
if(entryHour<9)
{
model.push(result= "0"+(entryHour=entryHour+1).toString()+":"+"00");
}
else
{
model.push(result=(entryHour=entryHour+1).toString()+":"+"00");
}

//reset the vars
entry=0;
entryMinutes=0;

}
}

return model;
}

No comments: