登录
首页 >  Golang >  Go问答

解决golang中调用Date时缺少Location的问题

来源:stackoverflow

时间:2024-03-11 23:45:26 150浏览 收藏

对于一个Golang开发者来说,牢固扎实的基础是十分重要的,golang学习网就来带大家一点点的掌握基础知识点。今天本篇文章带大家了解《解决golang中调用Date时缺少Location的问题》,主要介绍了,希望对大家的知识积累有所帮助,快点收藏起来吧,否则需要时就找不到了!

问题内容

我编写了一个 golang 程序来生成不同地点的月份开始日期和结束日期

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])
}

但是当我运行这段代码时,它会给我以下错误:-

时间:通话中缺少位置 日期

为什么会产生此错误以及如何解决此错误?

请提出任何建议!


解决方案


应该是 america/phoenix,而不是 america/pheonix

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

有关可用时区字符串列表的更多信息,请参阅:https://golang.org/src/time/zoneinfo_abbrs_windows.go

本篇关于《解决golang中调用Date时缺少Location的问题》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>