github.com/acwpower/studygo@v0.0.0-20220210033106-9f3f6e60fdcc/src/step01/random_dates/random_date.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"math/rand"
     6  	"time"
     7  )
     8  
     9  func main() {
    10  	rand.Seed(time.Now().Unix())
    11  	for count:=0;count<10;count++{
    12  		year := rand.Intn(100)+2001
    13  		leap := year%400==0||(year%100!=0&&year%4==0)
    14  		month := rand.Intn(12)+1
    15  		//month:=2
    16  		daypremonth :=31
    17  		switch month {
    18  		case 1,3,5,7,8,10,12:
    19  			daypremonth = 31
    20  		case 4,6,9,11:
    21  			daypremonth=30
    22  		default:
    23  			if leap {
    24  				daypremonth=29
    25  			}else{
    26  				daypremonth=28
    27  			}
    28  		}
    29  		fmt.Println(year,month,daypremonth)
    30  	}
    31  
    32  
    33  }