Skip to content

Instantly share code, notes, and snippets.

@ShiraazMoollatjie
Created August 3, 2020 18:37
Show Gist options
  • Save ShiraazMoollatjie/f2db5e4a1039d1756ad9c7c7b70fb867 to your computer and use it in GitHub Desktop.
Save ShiraazMoollatjie/f2db5e4a1039d1756ad9c7c7b70fb867 to your computer and use it in GitHub Desktop.
go: generate a random date based on a range
n := time.Now()
rand.Seed(n.UnixNano())
// Keep track of the minimum and maximum age
e := n.AddDate(-minAge, 0, 0).Unix()
s := n.AddDate(-maxAge, 0, 0).Unix()
// Take the delta of the date and print it out, return it, whatever
sec := rand.Int63n(e-s) + s
fmt.Sprintf("%v", time.Unix(sec, 0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment