Created
August 3, 2020 18:37
-
-
Save ShiraazMoollatjie/f2db5e4a1039d1756ad9c7c7b70fb867 to your computer and use it in GitHub Desktop.
go: generate a random date based on a range
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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