luoheng
10/26/2019 - 4:18 AM

dayOfYear


import (
    "strings"
    "strconv"
)

func dayOfYear(date string) int {
    normalYear := [13]int{0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}
    ymd := strings.Split(date, "-")
    year, _ := strconv.Atoi(ymd[0])
    month, _ := strconv.Atoi(ymd[1])
    day, _ := strconv.Atoi(ymd[2])
    d := normalYear[month] + day
    if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0) && month >= 3{
        return d + 1
    }
    return d
}