🐕

【Go】timeパッケージのAfter, Before, Equalの使い方

2024/11/01に公開

概要

Goの日時比較のサンプルコードをまとめたもの

サンプルコード

time.After()・・・timeAがtimeBを超える日時の場合にtrue

func main() {
	timeA := time.Date(2024, 1, 15, 0, 0, 0, 0, time.UTC)
	timeB := time.Date(2024, 1, 10, 0, 0, 0, 0, time.UTC)

	result := timeA.After(timeB)
	fmt.Println(result) // true
}

func main() {
	timeA := time.Date(2024, 1, 15, 0, 0, 0, 0, time.UTC)
	timeB := time.Date(2024, 1, 20, 0, 0, 0, 0, time.UTC)

	result := timeA.After(timeB)
	fmt.Println(result) // false
}

func main() {
	timeA := time.Date(2024, 1, 15, 0, 0, 0, 0, time.UTC)
	timeB := time.Date(2024, 1, 15, 0, 0, 0, 0, time.UTC)

	result := timeA.After(timeB)
	fmt.Println(result) // false
}

time.Before()・・・timeAがtimeBより前の日時の場合にtrue

func main() {
	timeA := time.Date(2024, 1, 15, 0, 0, 0, 0, time.UTC)
	timeB := time.Date(2024, 1, 20, 0, 0, 0, 0, time.UTC)

	result := timeA.Before(timeB)
	fmt.Println(result) // true
}

func main() {
	timeA := time.Date(2024, 1, 15, 0, 0, 0, 0, time.UTC)
	timeB := time.Date(2024, 1, 10, 0, 0, 0, 0, time.UTC)

	result := timeA.Before(timeB)
	fmt.Println(result) // false
}

func main() {
	timeA := time.Date(2024, 1, 15, 0, 0, 0, 0, time.UTC)
	timeB := time.Date(2024, 1, 15, 0, 0, 0, 0, time.UTC)

	result := timeA.Before(timeB)
	fmt.Println(result) // false
}

!time.Before()・・・timeAがtimeB以降の日時の場合にtrue(time.After() || time.Equal()と同じ)

func main() {
	timeA := time.Date(2024, 1, 15, 0, 0, 0, 0, time.UTC)
	timeB := time.Date(2024, 1, 10, 0, 0, 0, 0, time.UTC)

	result := !timeA.Before(timeB)
	fmt.Println(result) // true
}

func main() {
	timeA := time.Date(2024, 1, 15, 0, 0, 0, 0, time.UTC)
	timeB := time.Date(2024, 1, 20, 0, 0, 0, 0, time.UTC)

	result := !timeA.Before(timeB)
	fmt.Println(result) // false
}

func main() {
	timeA := time.Date(2024, 1, 15, 0, 0, 0, 0, time.UTC)
	timeB := time.Date(2024, 1, 15, 0, 0, 0, 0, time.UTC)

	result := !timeA.Before(timeB)
	fmt.Println(result) // true
}

// time.After() || time.Equal()
func main() {
	timeA := time.Date(2024, 1, 15, 0, 0, 0, 0, time.UTC)
	timeB := time.Date(2024, 1, 10, 0, 0, 0, 0, time.UTC)

	result := timeA.After(timeB) || timeA.Equal(timeB)
	fmt.Println(result) // true
}

!time.After()・・・timeAがtimeB以前の日時の場合にtrue(time.Before() || time.Equal()と同じ)

func main() {
	timeA := time.Date(2024, 1, 15, 0, 0, 0, 0, time.UTC)
	timeB := time.Date(2024, 1, 20, 0, 0, 0, 0, time.UTC)

	result := !timeA.After(timeB)
	fmt.Println(result) // true
}

func main() {
	timeA := time.Date(2024, 1, 15, 0, 0, 0, 0, time.UTC)
	timeB := time.Date(2024, 1, 10, 0, 0, 0, 0, time.UTC)

	result := !timeA.After(timeB)
	fmt.Println(result) // false
}

func main() {
	timeA := time.Date(2024, 1, 15, 0, 0, 0, 0, time.UTC)
	timeB := time.Date(2024, 1, 15, 0, 0, 0, 0, time.UTC)

	result := !timeA.After(timeB)
	fmt.Println(result) // true
}

// time.Before() || time.Equal()
func main() {
	timeA := time.Date(2024, 1, 15, 0, 0, 0, 0, time.UTC)
	timeB := time.Date(2024, 1, 20, 0, 0, 0, 0, time.UTC)

	result := timeA.Before(timeB) || timeA.Equal(timeB)
	fmt.Println(result) // true
}

time.Equal()・・・timeAがtimeBと同じ日時の場合true

func main() {
	timeA := time.Date(2024, 1, 15, 0, 0, 0, 0, time.UTC)
	timeB := time.Date(2024, 1, 15, 0, 0, 0, 0, time.UTC)

	result := timeA.Equal(timeB)
	fmt.Println(result) // true
}

func main() {
	timeA := time.Date(2024, 1, 15, 0, 0, 0, 0, time.UTC)
	timeB := time.Date(2024, 1, 10, 0, 0, 0, 0, time.UTC)

	result := timeA.Equal(timeB)
	fmt.Println(result) // false
}

func main() {
	timeA := time.Date(2024, 1, 15, 0, 0, 0, 0, time.UTC)
	timeB := time.Date(2024, 1, 20, 0, 0, 0, 0, time.UTC)

	result := timeA.Equal(timeB)
	fmt.Println(result) // false
}
GitHubで編集を提案

Discussion