Golang 将日期字符串转换为星期几

更新日期: 2021-05-09 阅读次数: 237 字数: 85 分类: golang

由于 Golang Weekday 返回的是日期的英文周几,需要加个 Map 转换为中文。

package main

import (
	"fmt"
	"time"
)

var WeekDayMap = map[string]string{
	"Monday":    "周一",
	"Tuesday":   "周二",
	"Wednesday": "周三",
	"Thursday":  "周四",
	"Friday":    "周五",
	"Saturday":  "周六",
	"Sunday":    "周日",
}

func main() {
	date := "2021-05-07"
	t, err := time.Parse("2006-01-02", date)
	if err != nil {
		panic(err)
	}
	fmt.Println(t.Weekday())
	fmt.Println(WeekDayMap[t.Weekday().String()])
}

运行输出结果:

Friday
周五

爱评论不评论