Skip to content

Instantly share code, notes, and snippets.

@nicewook
Last active February 8, 2024 21:50
Show Gist options
  • Save nicewook/097feded14e1132ce15cfa5b432097e7 to your computer and use it in GitHub Desktop.
Save nicewook/097feded14e1132ce15cfa5b432097e7 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"time"
)
func main() {
// 날짜와 시간을 모두 파싱 (time.Local 사용)
dateTimeStr := "2024-01-01 09:10:20"
dateTimeLayout := "2006-01-02 15:04:05"
parsedDateTime, _ := time.ParseInLocation(dateTimeLayout, dateTimeStr, time.Local)
fmt.Printf("Type: %T, Parsed as: %v\n", parsedDateTime, parsedDateTime)
// 날짜만 파싱 (time.Local 사용)
dateOnlyStr := "2024-01-01" // 날짜만 포함된 문자열
dateOnlyLayout := "2006-01-02" // 날짜만 파싱하기 위한 layout
parsedDateOnly, _ := time.ParseInLocation(dateOnlyLayout, dateOnlyStr, time.Local)
fmt.Printf("Type: %T, Parsed as: %v\n", parsedDateOnly, parsedDateOnly)
// time.Local이 Asia/Seoul로 설정되어 있을 때의 출력 결과
// Type: time.Time, Parsed as: 2024-01-01 09:10:20 +0900 KST
// Type: time.Time, Parsed as: 2024-01-01 00:00:00 +0900 KST
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment