Skip to main content
Known Participant
October 8, 2021
Answered

Populate Dropdown With Times

  • October 8, 2021
  • 3 replies
  • 2400 views

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.

Correct answer try67

Sure. Just change the first line to:

var times = [""];

3 replies

try67
Community Expert
Community Expert
October 8, 2021

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);
BrentJMAuthor
Known Participant
October 8, 2021

That worked, thanks for you help

 

Bernd Alheit
Community Expert
Community Expert
October 8, 2021

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

Nesa Nurani
Community Expert
Community Expert
October 8, 2021

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.

try67
Community Expert
Community Expert
October 8, 2021

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.