0
0

More than 3 years have passed since last update.

Go言語で年度を取得する

Posted at

日本だけだと思いますが、年度という概念を使わないといけない状況に陥ったので書いてみました。
nend_getterに「年」と「月」を渡せば、年度が返ってくるっていうだけのものです。

コードの全文です。

package main

import (
    "fmt"
    "time"
)

func nend_getter(year int, month int) int {
    var set_year int
    if month <= 3 {
        // 3月以前ならば、西暦から1年引く
        set_year = year - 1
    } else {
        set_year = year
    }
    return set_year
}

func main() {
    // Timezoneの設定
    time.Local = time.FixedZone("JST", 9*60*60)
    time.LoadLocation("JST")
    // 本日の表示操作
    fmt.Println("Year:", time.Now().Year())
    fmt.Println("Month:", int(time.Now().Month()))
    fmt.Println("Day:", time.Now().Day())

    //現在の年度
    nendo := nend_getter(time.Now().Year(), int(time.Now().Month()))
    fmt.Println("nendo : ", nendo)

    // 2000年4月の場合 -> 2000
    nendo = nend_getter(2000, 4)
    fmt.Println("nendo : ", nendo)
    // 2000年3月の場合 -> 1999
    nendo = nend_getter(2000, 3)
    fmt.Println("nendo : ", nendo)
}

上記を実行するとこんな感じになります。

$ go run main.go
Year: 2021
Month: 9
Day: 1
nendo :  2021
nendo :  2000
nendo :  1999

使う機会は少なそう。

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