2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

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

Posted at

概要

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
}
2
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?