dos3018 2018-10-31 06:23
浏览 72
已采纳

如何解决golang中“调用日期时缺少位置”的错误[关闭]

I have written a golang program to generate the start date and end date of the month for different locations

if month != "" && year != "" {
    var monthInt, _ = strconv.Atoi(month)
    var yearInt, _ = strconv.Atoi(year)
    timeZone, err := time.LoadLocation("America/Pheonix")
    if err != nil {
        fmt.Println(err)// nil
    }
    // currentLocation := time.Now().Location(timeZone) // when I use this it will works 
    // both are of same type 
    fmt.Println(reflect.TypeOf(timeZone))
    fmt.Println(reflect.TypeOf(time.UTC))
    firstOfMonth := time.Date(yearInt, time.Month(monthInt), 1, 0, 0, 0, 0, timeZone)
    onlyStartDate := strings.Split(firstOfMonth.Format("2006-01-02 00:00:00 -0000"), " ")
    lastOfMonth := firstOfMonth.AddDate(0, 1, -1).Format("2006-01-02 00:00:00 -0000")
    onlyLastDate := strings.Split(lastOfMonth, " ")
    merchantDb.GetProvidersOfTheMonth(onlyStartDate[0], onlyLastDate[0])
}

But when I run this code it will give me the error of the:-

time: missing Location in call to Date

Why this error is generating and how I will solve this error?

Please any suggestions!

  • 写回答

1条回答 默认 最新

  • duanmei1922 2018-10-31 06:33
    关注

    It should be America/Phoenix, not America/Pheonix.

    timeZone, _ := time.LoadLocation("America/Phoenix")
    

    For more information about list of available time zones string, please see: https://golang.org/src/time/zoneinfo_abbrs_windows.go

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?