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

Generate Random Timestamps in Go

Complete code tutorial with examples and best practices

[ Code Example - Quick Summary ]

Language: Go

What: Generate random Unix timestamps in Go using time.Now() and math/rand. Covers current time, past/future timestamps, and formatting for testing.

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

Generate random Unix timestamps in Go using time.Now() and math/rand. Covers current time, past/future timestamps, and formatting 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"
    "time"
)

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

// Generate current Unix timestamp (seconds)
func currentTimestamp() int64 {
    return time.Now().Unix()
}

// Generate current Unix timestamp (milliseconds)
func currentTimestampMillis() int64 {
    return time.Now().UnixMilli()
}

// Generate random timestamp between two dates
func randomTimestamp(startDate, endDate string) (int64, error) {
    layout := "2006-01-02"
    start, err := time.Parse(layout, startDate)
    if err != nil {
        return 0, err
    }
    end, err := time.Parse(layout, endDate)
    if err != nil {
        return 0, err
    }

    delta := end.Unix() - start.Unix()
    return start.Unix() + rand.Int63n(delta), nil
}

// Generate random past timestamp (within N days)
func randomPastTimestamp(daysAgo int) int64 {
    now := time.Now().Unix()
    seconds := int64(daysAgo * 86400)
    return now - rand.Int63n(seconds)
}

// Generate random future timestamp (within N days)
func randomFutureTimestamp(daysAhead int) int64 {
    now := time.Now().Unix()
    seconds := int64(daysAhead * 86400)
    return now + rand.Int63n(seconds)
}

// Convert timestamp to readable format
func formatTimestamp(timestamp int64) string {
    return time.Unix(timestamp, 0).Format("2006-01-02 15:04:05")
}

// Generate multiple random timestamps
func generateTimestamps(count int, startDate, endDate string) []int64 {
    timestamps := make([]int64, count)
    for i := 0; i < count; i++ {
        ts, _ := randomTimestamp(startDate, endDate)
        timestamps[i] = ts
    }
    return timestamps
}

func main() {
    fmt.Println("Current:", currentTimestamp())

    random, _ := randomTimestamp("2023-01-01", "2024-12-31")
    fmt.Println("Random:", random)

    past := randomPastTimestamp(30)
    fmt.Printf("Past: %d (%s)\n", past, formatTimestamp(past))

    future := randomFutureTimestamp(30)
    fmt.Printf("Future: %d (%s)\n", future, formatTimestamp(future))

    fmt.Print("Batch: ")
    batch := generateTimestamps(3, "2023-01-01", "2024-12-31")
    for i, ts := range batch {
        fmt.Print(ts)
        if i < len(batch)-1 {
            fmt.Print(", ")
        }
    }
    fmt.Println()
}

[EXPLANATION]

Go time.Now().Unix() returns current Unix timestamp in seconds. time.Parse() parses date strings using Go layout format ("2006-01-02" is reference date). rand.Int63n() generates 64-bit random integers. time.Unix() converts timestamp to Time. Format() uses reference date for formatting. Go's time package uses UTC by default.

Expected Output

Current: 1703001234
Random: 1698765432
Past: 1700445678 (2023-11-19 14:27:58)
Future: 1706123456 (2024-01-24 18:10:56)
Batch: 1680123456, 1685432100, 1692345678

Common Use Cases

  • Generate timestamps for Go microservices testing
  • Create event timestamps for gRPC service testing
  • Generate log timestamps for structured logging
  • Test timestamp-based rate limiting in APIs
  • Create expiration timestamps for JWT tokens

Important Notes

  • Go reference date: "2006-01-02 15:04:05" (Mon Jan 2 15:04:05 MST 2006)
  • time.Now().UTC() ensures UTC timestamps
  • UnixMilli() and UnixNano() available in Go 1.17+
  • rand.Seed() should be called once in init()
  • Use time.UTC location for consistent UTC parsing

Try Our Interactive Generator

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

TRY TIMESTAMPS GENERATOR →