Developer Tools for Random Data Generation // v2.6.1
root@generate-random:~/times/go$ _

Generate Random Times in Go

Complete code tutorial with examples and best practices

[ Code Example - Quick Summary ]

Language: Go

What: Generate random times in Go using math/rand and time package. Covers time generation, formatting, and validation for testing.

Try it: Use our interactive Times generator or integrate this code into your Go application.

Generate random times in Go using math/rand and time package. Covers time generation, formatting, and validation for testing. Looking for other languages? Check our code examples in PHP , JavaScript , Python , Java , C# , C++ and Ruby or use our interactive web generator.

Go Code Example

package main

import (
    "fmt"
    "math/rand"
    "regexp"
    "time"
)

func init() {
    rand.Seed(time.Now().UnixNano())
}

// Generate random time (24-hour format HH:MM:SS)
func randomTime24() string {
    hour := rand.Intn(24)
    minute := rand.Intn(60)
    second := rand.Intn(60)
    return fmt.Sprintf("%02d:%02d:%02d", hour, minute, second)
}

// Generate random time (12-hour format with AM/PM)
func randomTime12() string {
    hour := rand.Intn(12) + 1
    minute := rand.Intn(60)
    ampm := []string{"AM", "PM"}[rand.Intn(2)]
    return fmt.Sprintf("%02d:%02d %s", hour, minute, ampm)
}

// Generate random time within business hours (9 AM - 5 PM)
func randomBusinessTime() string {
    hour := rand.Intn(9) + 9 // 9-17
    minute := rand.Intn(60)
    return fmt.Sprintf("%02d:%02d", hour, minute)
}

// Generate random time as time.Time object (today with random time)
func randomTimeObject() time.Time {
    now := time.Now()
    return time.Date(
        now.Year(), now.Month(), now.Day(),
        rand.Intn(24), rand.Intn(60), rand.Intn(60), 0,
        time.Local,
    )
}

// Validate time format (HH:MM:SS)
func isValidTime(timeStr string) bool {
    pattern := `^([01]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$`
    matched, _ := regexp.MatchString(pattern, timeStr)
    return matched
}

// Generate multiple random times
func generateTimes(count int) []string {
    times := make([]string, count)
    for i := 0; i < count; i++ {
        times[i] = randomTime24()
    }
    return times
}

func main() {
    fmt.Println("24-hour:", randomTime24())
    fmt.Println("12-hour:", randomTime12())
    fmt.Println("Business:", randomBusinessTime())
    fmt.Println("Time object:", randomTimeObject().Format("15:04:05"))
    fmt.Println("Valid:", isValidTime("14:30:45"))
    fmt.Print("Batch: ")
    batch := generateTimes(3)
    for i, t := range batch {
        fmt.Print(t)
        if i < len(batch)-1 {
            fmt.Print(", ")
        }
    }
    fmt.Println()
}

[EXPLANATION]

Go uses rand.Intn() for hours (0-23), minutes (0-59), seconds (0-59). fmt.Sprintf("%02d") formats with leading zeros. time.Date() creates time.Time with specific time. Format("15:04:05") uses Go reference time for formatting. regexp.MatchString() validates format. make([]string, count) pre-allocates slices.

Expected Output

24-hour: 14:23:45
12-hour: 09:15 AM
Business: 11:30
Time object: 14:23:45
Valid: true
Batch: 08:45:12, 16:20:33, 22:10:05

Common Use Cases

  • Generate times for Go microservices testing
  • Create time fields for gRPC service testing
  • Generate appointment times for scheduling APIs
  • Test time validation in web services
  • Create working hours for time tracking systems

Important Notes

  • Go reference time: "15:04:05" for 24-hour, "03:04 PM" for 12-hour
  • fmt.Sprintf("%02d") formats integers with leading zeros
  • rand.Seed() should be called once in init()
  • time.Date() creates time.Time with all components
  • regexp.MatchString() compiles regex each call; use Compile() for reuse

Try Our Interactive Generator

Don't want to write code? Use our free web-based Times generator with instant results.

TRY TIMES GENERATOR →