Golang

[Golang]3달 전 날짜 구하기 - AddDate()

stayhungri 2022. 7. 15. 00:17

특정 날짜에서 3달 전 혹은 2주 전 등의 날짜를 구하고 싶다면 time 패키지에 AddDate() 함수를 이용하면 됩니다.

 

func threeMonthAgo() {
	t := time.Now()
	t2 := t.AddDate(0, -3, 0)

	fmt.Printf("now          : %+v\n", t)
	fmt.Printf("threeMonthAgo: %+v\n", t2)
}

now          : 2022-07-15 00:16:43.869676 +0900 KST m=+0.000060240
threeMonthAgo: 2022-04-15 00:16:43.869676 +0900 KST