nugawiki

./categories/back-end

Go time 패키지

업데이트 2026-07-21 조회수

생성

time.Now()
time.Date(year, month, day, h, m, s, ns, loc)
time.Unix(sec, nsec)
  • Month: 1(January)부터 (JS는 0부터)
  • time.UTC: UTC Location

조회

t := time.Now()
t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second()
t.Weekday(), t.Unix(), t.Nanosecond(), t.UnixNano()

포맷 (레이아웃: Mon Jan 2 15:04:05 -0700 MST 2006)

time.Now().Format("2006-01-02 15:04:05")
time.Parse("2006-01-02 15:04:05", s)

상수: time.RFC3339, time.ANSIC, time.Kitchen

타임존

loc, _ := time.LoadLocation("Asia/Seoul")
kst := utc.In(loc)
kst.UTC()
time.ParseInLocation(layout, value, loc)

Duration / 비교

time.Second, time.Minute, time.Hour
t.Add(time.Hour * 10)
t.Before(other) / t.After(other) / t.Equal(other)

Timer / Ticker

t1 := time.NewTimer(time.Second * 2)
<-t1.C
t1.Stop()

ticker := time.NewTicker(time.Second)
go func() {
  for t := range ticker.C { fmt.Println(t) }
}()
ticker.Stop()
  • Timer: 1회 / Ticker: 주기

./comments