LoginSignup
0
0

More than 1 year has passed since last update.

Goで日本語曜日を含む日付format

Last updated at Posted at 2022-02-10

参考元

以下記事にて記載されてますが、少しだけ改良?よくしたので共有。

改良点
・レシーバーにした

package main

import (
    "log"
    "strings"
    "time"
)

type Time struct {
    time.Time
}

const (
    yyyym   int = 1 // 2009年11月
    yyyymdw int = 2 // 2009年11月10日(火曜日)
)

func (t Time) GetJpnFormatTime(layout int) (jft string) {
    weekdayja := strings.NewReplacer(
        "Sun", "日",
        "Mon", "月",
        "Tue", "火",
        "Wed", "水",
        "Thu", "木",
        "Fri", "金",
        "Sat", "土",
    )

    var l string
    switch layout {
    case yyyym:
        l = "2006年1月" // 2009年11月
    case yyyymdw:
        l = "2006年1月2日(Mon曜日)" // 2009年11月10日(火曜日)
    }
    jft = weekdayja.Replace(t.Format(l))
    return jft
}

func main() {
    t := Time{}
    t.Time = time.Now()
    yyymmdd := t.GetJpnFormatTime(yyyym)
    log.Print(yyymmdd)
}

それだけ

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0