Skip to main content
Known Participant
Oct 8, 2021 2:45 PM UTC
Answered

Populate Dropdown With Times

I am trying to populate a dropdown box with times from 12:00 am to 11:59 pm in fifteen minute increments.  I know that I could manually populate the dropdown box with this information, however I was wondering if there was a way that I could populate it with javascript.  I am fairly new to javascript, so I will need a detailed guide.

try67
try67Correct answer
Community Expert
Aug 26, 2025 9:44 PM UTC

Sure. Just change the first line to:

var times = [""];

10 replies

try67
Community Expert
Community Expert
Oct 8, 2021 3:50 PM UTC

You can do it using this code:

 

var times = [];
for (var i=0; i<=11; i++) {
	for (var j=0; j<=45; j+=15) {
		var hour = (i<10) ? "0"+i : i;
		var minute = (j<10) ? "0"+j : j;
		times.push(hour + ":" + minute + " am")
	}
}
for (var j=0; j<=45; j+=15) {
	var hour = "12";
	var minute = (j<10) ? "0"+j : j;
	times.push(hour + ":" + minute + " pm")
}
for (var i=1; i<=11; i++) {
	for (var j=0; j<=45; j+=15) {
		var hour = (i<10) ? "0"+i : i;
		var minute = (j<10) ? "0"+j : j;
		times.push(hour + ":" + minute + " pm")
	}
}
this.getField("Time").setItems(times);
Translate
BrentJMAuthor
Known Participant
Oct 8, 2021 4:13 PM UTC

That worked, thanks for you help

 

Translate
Bernd Alheit
Community Expert
Community Expert
Oct 8, 2021 3:17 PM UTC

With a script you can create an array and use the array at setItems.

Translate
Nesa Nurani
Community Expert
Community Expert
Oct 8, 2021 3:05 PM UTC

Use something like this in a button:

this.getField("Dropdown").setItems([["12:00 am"],["12:15 am"],["12:30 am"]]);

 

Change "Dropdown" to your actual dropdown field name and add more times.

Translate
try67
Community Expert
Community Expert
Oct 8, 2021 3:39 PM UTC

You don't need to place each item in its own array, unless you want it to also have an export value which is different from the display value.

Translate