yyyyMMddHHmmss 형식을 사용하여 현재 시간을 포맷하는 방법은 무엇입니까?
이 형식을 사용하여 현재 시간을 형식화하려고합니다 yyyyMMddHHmmss.
t := time.Now()
fmt.Println(t.Format("yyyyMMddHHmmss"))
다음과 같이 출력됩니다.
yyyyMMddHHmmss
어떤 제안?
사용하다
fmt.Println(t.Format("20060102150405"))
Go는 다음 상수를 사용하여 날짜 형식을 지정하므로 여기를 참조 하십시오.
const (
stdLongMonth = "January"
stdMonth = "Jan"
stdNumMonth = "1"
stdZeroMonth = "01"
stdLongWeekDay = "Monday"
stdWeekDay = "Mon"
stdDay = "2"
stdUnderDay = "_2"
stdZeroDay = "02"
stdHour = "15"
stdHour12 = "3"
stdZeroHour12 = "03"
stdMinute = "4"
stdZeroMinute = "04"
stdSecond = "5"
stdZeroSecond = "05"
stdLongYear = "2006"
stdYear = "06"
stdPM = "PM"
stdpm = "pm"
stdTZ = "MST"
stdISO8601TZ = "Z0700" // prints Z for UTC
stdISO8601ColonTZ = "Z07:00" // prints Z for UTC
stdNumTZ = "-0700" // always numeric
stdNumShortTZ = "-07" // always numeric
stdNumColonTZ = "-07:00" // always numeric
)
이 질문은 "golang 현재 시간 형식"을 찾을 때 Google 검색의 맨 위에 표시되므로 다른 형식을 사용하려는 모든 사람들은 언제든지 다음으로 전화 할 수 있습니다.
t := time.Now()
t.Year()
t.Month()
t.Day()
t.Hour()
t.Minute()
t.Second()
예를 들어 현재 날짜 시간을 "YYYY-MM-DDTHH : MM : SS"(예 : 2019-01-22T12 : 40 : 55)로 가져 오려면 fmt.Sprintf와 함께 다음 메서드를 사용할 수 있습니다.
t := time.Now()
formatted := fmt.Sprintf("%d-%02d-%02dT%02d:%02d:%02d",
t.Year(), t.Month(), t.Day(),
t.Hour(), t.Minute(), t.Second())
항상 그렇듯이 문서가 최고의 학습 소스임을 기억하십시오 : https://golang.org/pkg/time/
Golang의 시간 패키지에는 살펴볼만한 몇 가지 방법이 있습니다.
func (시간) 형식
func (t Time) Format(layout string) string Format returns a textual representation of the time value formatted according to layout, which defines the format by showing how the reference time,
Mon Jan 2 15:04:05 -0700 MST 2006 would be displayed if it were the value; it serves as an example of the desired output. The same display rules will then be applied to the time value. Predefined layouts ANSIC, UnixDate, RFC3339 and others describe standard and convenient representations of the reference time. For more information about the formats and the definition of the reference time, see the documentation for ANSIC and the other constants defined by this package.
Source (http://golang.org/pkg/time/#Time.Format)
I also found an example of defining the layout (http://golang.org/src/pkg/time/example_test.go)
func ExampleTime_Format() {
// layout shows by example how the reference time should be represented.
const layout = "Jan 2, 2006 at 3:04pm (MST)"
t := time.Date(2009, time.November, 10, 15, 0, 0, 0, time.Local)
fmt.Println(t.Format(layout))
fmt.Println(t.UTC().Format(layout))
// Output:
// Nov 10, 2009 at 3:00pm (PST)
// Nov 10, 2009 at 11:00pm (UTC)
}
import("time")
layout := "2006-01-02T15:04:05.000Z"
str := "2014-11-12T11:45:26.371Z"
t, err := time.Parse(layout, str)
if err != nil {
fmt.Println(err)
}
fmt.Println(t)
gives:
>> 2014-11-12 11:45:26.371 +0000 UTC
'IT박스' 카테고리의 다른 글
| 라텍스는 목록의 항목 사이에 공백을 제거합니다. (0) | 2020.09.05 |
|---|---|
| SQL 명령을 통해 MySQL 호스트 표시 (0) | 2020.09.05 |
| float를 최대 / 최소 값으로 어떻게 초기화합니까? (0) | 2020.09.05 |
| PHP로 IP 주소 국가 가져 오기 (0) | 2020.09.05 |
| 터미널에서 Ruby 코드를 실행하는 방법은 무엇입니까? (0) | 2020.09.05 |