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

Generate Random Dates in Go

Complete code tutorial with examples and best practices

[ Code Example - Quick Summary ]

Language: Go

What: Generate random dates in Go using time package. Covers time.Time, date ranges, formatting, timezone handling, and random date generation for microservices testing.

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

Generate random dates in Go using time package. Covers time.Time, date ranges, formatting, timezone handling, and random date generation for microservices 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"
    "time"
)

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

// Generate random date between two dates
func randomDate(startDate, endDate string) time.Time {
    layout := "2006-01-02"
    start, _ := time.Parse(layout, startDate)
    end, _ := time.Parse(layout, endDate)

    delta := end.Unix() - start.Unix()
    randomSeconds := rand.Int63n(delta + 1)

    return start.Add(time.Duration(randomSeconds) * time.Second)
}

// Generate random past date (within last N days)
func randomPastDate(daysAgo int) time.Time {
    now := time.Now()
    maxSeconds := int64(daysAgo * 86400)
    randomSeconds := rand.Int63n(maxSeconds + 1)
    return now.Add(-time.Duration(randomSeconds) * time.Second)
}

// Generate random future date (within next N days)
func randomFutureDate(daysAhead int) time.Time {
    now := time.Now()
    maxSeconds := int64(daysAhead * 86400)
    randomSeconds := rand.Int63n(maxSeconds + 1)
    return now.Add(time.Duration(randomSeconds) * time.Second)
}

// Generate random birthday (18-80 years ago)
func randomBirthday() time.Time {
    now := time.Now()
    minAge := 18
    maxAge := 80
    yearsAgo := rand.Intn(maxAge-minAge+1) + minAge
    birthYear := now.Year() - yearsAgo

    startDate := fmt.Sprintf("%d-01-01", birthYear)
    endDate := fmt.Sprintf("%d-12-31", birthYear)

    return randomDate(startDate, endDate)
}

// Generate random time with specific components
func randomDateTime(startDate, endDate string) time.Time {
    date := randomDate(startDate, endDate)
    hour := rand.Intn(24)
    minute := rand.Intn(60)
    second := rand.Intn(60)

    return time.Date(
        date.Year(), date.Month(), date.Day(),
        hour, minute, second, 0, time.UTC,
    )
}

// Generate multiple random dates
func generateRandomDates(count int, startDate, endDate string) []string {
    dates := make([]string, count)
    layout := "2006-01-02"

    for i := 0; i < count; i++ {
        dates[i] = randomDate(startDate, endDate).Format(layout)
    }

    return dates
}

func main() {
    layout := "2006-01-02"
    layoutWithTime := "2006-01-02 15:04:05"

    fmt.Println("Random date:", randomDate("2020-01-01", "2025-12-31").Format(layout))
    fmt.Println("Past date:", randomPastDate(30).Format(layout))
    fmt.Println("Future date:", randomFutureDate(90).Format(layout))
    fmt.Println("Random birthday:", randomBirthday().Format(layout))
    fmt.Println("With time:", randomDateTime("2024-01-01", "2024-12-31").Format(layoutWithTime))
    fmt.Println("Batch:", generateRandomDates(5, "2020-01-01", "2025-12-31"))
}

[EXPLANATION]

Go's time package uses time.Time for date/time representation. time.Parse() parses strings using layout "2006-01-02" (Go's reference time). Unix() returns seconds since 1970-01-01. Add() and Sub() perform date arithmetic with time.Duration. Format() converts to strings using the same reference layout. Go's timezone support uses time.LoadLocation(). Always call rand.Seed() once at program start.

Expected Output

Random date: 2023-07-15
Past date: 2024-11-17
Future date: 2025-03-15
Random birthday: 1985-04-23
With time: 2024-07-15 14:32:47
Batch: [2022-03-12 2024-08-05 2021-11-28 2023-05-19 2025-01-07]

Common Use Cases

  • Generate test timestamps for Go microservices and APIs
  • Create sample data for database seeding in Go applications
  • Test date-based filtering and sorting logic
  • Generate time-series data for metrics and monitoring
  • Populate structs with random dates for unit testing

Important Notes

  • Go's reference time is "Mon Jan 2 15:04:05 MST 2006" (01/02 03:04:05PM '06 -0700)
  • time.Now() returns local time; use time.Now().UTC() for UTC
  • rand.Seed() should be called once at program start, not per function
  • For concurrent random generation, use rand.New(rand.NewSource(seed))
  • Go 1.20+ introduces rand.N() for simpler random integer generation

Try Our Interactive Generator

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

TRY DATES GENERATOR →