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

如何解决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

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

报告相同问题?

悬赏问题

  • ¥15 机器学习训练相关模型
  • ¥15 Todesk 远程写代码 anaconda jupyter python3
  • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?
  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制