1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| func dayOfYear(date string) int { year, _ := strconv.Atoi(date[:4]) month, _ := strconv.Atoi(date[5:7]) day, _ := strconv.Atoi(date[8:]) months := []int{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} daySum := 0 for i := 1; i < month; i++ { daySum += months[i - 1] } if month > 2 && isLeep(year) { daySum++ } return daySum + day }
func isLeep(year int) bool { return (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)) }
|