英文:
How to get a day of week from civil.Date
问题
如何使用civil.Date类型从civil.Date中获取星期几,例如星期一、星期日。
date := civil.Date{
    Year:  2021,
    Month: time.May,
    Day:   1}
我正在寻找与"time"包中的Weekday()函数等效的方法。
也欢迎提供其他替代方法。
英文:
How can I get a day of week from civil.Date using the type civil.Date such as Monday, Sunday.
	date := civil.Date{
	    Year:  2021,
	    Month: time.May,
	    Day:   1}
I am looking for the equivalent to Weekday() function of 'time' package.
Any alternative way is also welcomed.
答案1
得分: 2
package main
import (
"fmt"
"os"
"time"
"cloud.google.com/go/civil"
)
func main() {
date := civil.Date{
Year:  2021,
Month: time.September,
Day:   5,
}
t, err := time.Parse(time.RFC3339, date.String()+"T00:00:00Z")
if err != nil {
	fmt.Println(err)
	os.Exit(1)
}
fmt.Println(t.Weekday().String())
}
英文:
How about
package main
import (
	"fmt"
	"os"
	"time"
	"cloud.google.com/go/civil"
)
func main() {
	date := civil.Date{
		Year:  2021,
		Month: time.September,
		Day:   5,
	}
	t, err := time.Parse(time.RFC3339, date.String()+"T00:00:00Z")
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	fmt.Println(t.Weekday().String())
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论