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 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了