2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Goでその年の日数を数える

Posted at

うるう年だったら366, それ以外だったら365になるやつ。
うるう年判定を実装してもいいのだけど、t.YearDay()というメソッドがあったので、12月31日がYearDayで何日目か調べるという技がありそう。

package main

import (
	"fmt"
	"time"
)

func main() {
	for y := 2010; y < 2021; y++ {
		fmt.Println(y, CountYearDay(y))
	}
}

func CountYearDay(year int) int {
	lastday := time.Date(year, 12, 31, 0, 0, 0, 0, time.UTC)
	return lastday.YearDay()
}

実行結果

2010 365
2011 365
2012 366
2013 365
2014 365
2015 365
2016 366
2017 365
2018 365
2019 365
2020 366
2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?