LoginSignup
2
0

More than 5 years have passed since last update.

今が第*曜日かどうかを判定する

Posted at

第2土曜日とか、第3金曜日とか、第*曜日を知りたいときあると思います。

package main

import (
    "fmt"
    "time"
)

func main() {
    now := time.Now()
    count := getCountWeekday(now)
    fmt.Printf("第%d%s\n",count,now.Weekday())

    // 第2火曜日かどうか
    fmt.Println(count == 2 && now.Weekday() == time.Tuesday)
}

func getCountWeekday(now time.Time) int {
    count := 0
    for t := now; t.Month() == now.Month(); t = t.Add(-7 * 24 * time.Hour) {
        count++
    }
    return count
}

実行結果

第2Tuesday
true
2
0
1

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
0