[Go] Time format (2006-01-02 15:04:05)

2022. 7. 20. 22:35· Language/Go

Go 에서 time format 하는 방법

The layouts use the reference time on Mon Jan 2 15:04:05 MST 2006 to indicate
the pattern under which to format the time object.

 

layout 의 형태가 정해져있다.

2006-01-02 15:04:05 

 

이 형태를 알아두고 time format 하면 된다.

처음에 Go time format 이 layout 에 대해 잘 이해되지 않았는데 한번 해보면 다음부터는 헷갈리지 않고 바로 사용 할 수 있다.

 

package main

import (
	"fmt"
	"time"
)

func main() {
	t := time.Now()
	fmt.Println(t)
	fmt.Println(t.Format("2006-01-02 15:04:05"))
}

2022-07-20 22:27:03.411994 +0900 KST m=+0.000154999
2022-07-20 22:27:03

layout 이 정해져있고 원하는 형태로 만들어 주면 되는 것이다.

func main() {
	t := time.Now()
	fmt.Println(t.Format("15:04:05 01-02-2006"))
}

22:28:31 07-20-2022

layout 을 시간이 앞으로 년도가 제일 뒤로 해서 print 한 time format 

한번 알아두면 Go 에서 time format 에 대해 시간 낭비가 사라질 것이다.

 

 

참고로 Go 에서 제공하는 layout 

Use a format constants

Go provides in the time package some handy constants for commonly used formats:

const (
        ANSIC       = "Mon Jan _2 15:04:05 2006"
        UnixDate    = "Mon Jan _2 15:04:05 MST 2006"
        RubyDate    = "Mon Jan 02 15:04:05 -0700 2006"
        RFC822      = "02 Jan 06 15:04 MST"
        RFC822Z     = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone
        RFC850      = "Monday, 02-Jan-06 15:04:05 MST"
        RFC1123     = "Mon, 02 Jan 2006 15:04:05 MST"
        RFC1123Z    = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone
        RFC3339     = "2006-01-02T15:04:05Z07:00"
        RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
        Kitchen     = "3:04PM"
        // Handy time stamps.
        Stamp      = "Jan _2 15:04:05"
        StampMilli = "Jan _2 15:04:05.000"
        StampMicro = "Jan _2 15:04:05.000000"
        StampNano  = "Jan _2 15:04:05.000000000"
)

 

몇가지 예를 들어보면

func main() {
	t := time.Now()
	fmt.Println(t.Format(time.ANSIC))
	fmt.Println(t.Format(time.UnixDate))
	fmt.Println(t.Format(time.Stamp))
}

Wed Jul 20 22:32:58 2022
Wed Jul 20 22:32:58 KST 2022
Jul 20 22:32:58

 

Time format 은 정말 많이 사용하니까 알아두자!

 

 

 

 

'Language > Go' 카테고리의 다른 글

[Go] timestamp Asia/Seoul  (0) 2022.09.11
[Go] Gin vs Echo vs Fiber Framework  (0) 2022.08.21
[Go] Covert Time Unix to UTC  (1) 2022.07.20
[Go] env file ( godotenv )  (0) 2022.07.06
[Go] net Dial, DialTimeout (check tcp port open)  (0) 2022.07.04
'Language/Go' 카테고리의 다른 글
  • [Go] timestamp Asia/Seoul
  • [Go] Gin vs Echo vs Fiber Framework
  • [Go] Covert Time Unix to UTC
  • [Go] env file ( godotenv )
임쟌
임쟌
임쟌
Jian's Blog
임쟌
전체
오늘
어제

공지사항

  • [자기소개]
  • 쟌's Blog (227)
    • Language (32)
      • Python (8)
      • Go (24)
      • Java (0)
    • Framework (10)
      • Django (9)
      • Gin (1)
      • Spring boot (0)
      • Fiber (0)
    • Database (10)
      • PostgreSQL (8)
      • MySQL (0)
      • Redis (2)
    • Server (51)
      • Linux (16)
      • Git (12)
      • Oracle Cloud Infrastructure (13)
      • Mac (4)
      • Docker (4)
      • RabbitMQ (0)
      • ETC (2)
    • Operating System (0)
      • OS (0)
    • Algorithm (22)
      • Go (22)
      • Python (0)
    • Exam Certification (4)
    • Daily Life (27)
      • Review (21)
      • Diary (6)
    • 이공계전문기술연수 (71)
      • Java (17)
      • Database (8)
      • HTML | CSS (13)
      • JavaScript | jQuery (6)
      • Servlet | JSP (16)
      • Spring Framework (11)

인기 글

최근 댓글

최근 글

hELLO · Designed By 정상우.v4.2.2
임쟌
[Go] Time format (2006-01-02 15:04:05)
상단으로

티스토리툴바

개인정보

  • 티스토리 홈
  • 포럼
  • 로그인

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.