如何从 civil.Date 获取星期几

huangapple go评论142阅读模式
英文:

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())

}

huangapple
  • 本文由 发表于 2021年9月5日 16:09:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/69061622.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定