Generate Random Times in JavaScript
Complete code tutorial with examples and best practices
[ Code Example - Quick Summary ]
Language: JavaScript
What: Generate random times in JavaScript using Math.random() and string formatting. Covers 12-hour, 24-hour formats, and time validation.
Try it: Use our interactive Times generator or integrate this code into your JavaScript application.
Generate random times in JavaScript using Math.random() and string formatting. Covers 12-hour, 24-hour formats, and time validation. Looking for other languages? Check our code examples in PHP , Python , Java , C# , C++ , Ruby and Go or use our interactive web generator.
JavaScript Code Example
// Generate random time (24-hour format HH:MM:SS)
function randomTime24() {
const hour = Math.floor(Math.random() * 24);
const minute = Math.floor(Math.random() * 60);
const second = Math.floor(Math.random() * 60);
return `${hour.toString().padStart(2, "0")}:${minute.toString().padStart(2, "0")}:${second.toString().padStart(2, "0")}`;
}
// Generate random time (12-hour format with AM/PM)
function randomTime12() {
const hour = Math.floor(Math.random() * 12) + 1;
const minute = Math.floor(Math.random() * 60);
const ampm = Math.random() < 0.5 ? "AM" : "PM";
return `${hour.toString().padStart(2, "0")}:${minute.toString().padStart(2, "0")} ${ampm}`;
}
// Generate random time within business hours (9 AM - 5 PM)
function randomBusinessTime() {
const hour = Math.floor(Math.random() * 9) + 9; // 9-17
const minute = Math.floor(Math.random() * 60);
return `${hour.toString().padStart(2, "0")}:${minute.toString().padStart(2, "0")}`;
}
// Generate random time with seconds
function randomTimeWithSeconds() {
return `${Math.floor(Math.random() * 24).toString().padStart(2, "0")}:${Math.floor(Math.random() * 60).toString().padStart(2, "0")}:${Math.floor(Math.random() * 60).toString().padStart(2, "0")}`;
}
// Validate time format (HH:MM:SS)
function isValidTime(time) {
const pattern = /^([01]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$/;
return pattern.test(time);
}
// Generate multiple random times
function generateTimes(count = 10) {
return Array.from({ length: count }, () => randomTime24());
}
console.log("24-hour:", randomTime24());
console.log("12-hour:", randomTime12());
console.log("Business:", randomBusinessTime());
console.log("Valid:", isValidTime("14:30:45"));
console.log("Batch:", generateTimes(3).join(", "));
[EXPLANATION]
JavaScript uses Math.random() to generate hours (0-23), minutes (0-59), and seconds (0-59). padStart(2, "0") adds leading zeros. Math.floor() converts floats to integers. Template literals provide clean string formatting. Hours range 0-23 for 24-hour or 1-12 for 12-hour with AM/PM. RegExp.test() validates time format. Array.from() enables functional batch generation.
Expected Output
24-hour: 14:23:45 12-hour: 09:15 AM Business: 11:30 Valid: true Batch: 08:45:12, 16:20:33, 22:10:05
Common Use Cases
- Generate times for React/Vue scheduling components
- Create time pickers for frontend calendar applications
- Test time validation in web forms
- Generate random meeting times for productivity apps
- Create time-based test data for Node.js APIs
Important Notes
-
padStart(2, "0")ensures two-digit format (01, 02, etc.) -
Math.random()returns [0, 1), multiply by range for values -
Use
Intl.DateTimeFormatfor locale-specific time formatting - Business hours vary by region and industry
-
Dateobject can be used for more complex time operations
Try Our Interactive Generator
Don't want to write code? Use our free web-based Times generator with instant results.
TRY TIMES GENERATOR →Other Programming Languages
View Times generation code examples in PHP
View Times generation code examples in Python
View Times generation code examples in Java
View Times generation code examples in C#
View Times generation code examples in C++
View Times generation code examples in Ruby
View Times generation code examples in Go