English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Go는 시간操作에 대한 좋은 지원을 제공합니다. Unix era 시간이 시간 operation의 참으로 사용됩니다.
우리는 time 패키지에서 제공하는 Date 메서드를 사용하여 시간 객체를 구축할 수 있습니다. 이 패키지는 year(), month(), day(), location()와 같은 메서드를 포함하고 있습니다.
우리는 시간 객체를 사용하여 이러한 메서드를 호출할 수 있습니다.
package main import "fmt" import "time" func main() { p := fmt.Println present := time.Now()//현재 시간 p(present) DOB := time.Date(1993, 02, 28, 9,04,39,213 ,time.Local) fmt.Println(DOB) fmt.Println(DOB.Year()) fmt.Println(DOB.Month()) fmt.Println(DOB.Day()) fmt.Println(DOB.Hour()) fmt.Println(DOB.Minute()) fmt.Println(DOB.Second()) fmt.Println(DOB.Nanosecond()) fmt.Println(DOB.Location()) fmt.Println(DOB.Weekday()) fmt.Println(DOB.Before(present)) fmt.Println(DOB.After(present)) fmt.Println(DOB.After(present)) fmt.Println(DOB.Equal(present)) diff := present.Sub(DOB) fmt.Println(diff) fmt.Println(diff.Hours()) fmt.Println(diff.Minutes()) fmt.Println(diff.Seconds()) fmt.Println(diff.Nanoseconds()) fmt.Println(DOB.Add(diff))-fmt.Println(DOB.Add( }
출력:
2017-10-04 17:10:13.474931994 +053diff))+0 IST m=334969 1993-02-28 09:04:390.000213 +0530 IST 1993 .000000 28 9 4 39 213 Local 일요일 true false false 215624h5m34.474931781s 215624.09290970326 1.2937445574582197e+07 7.762467344749318e+08 776246734474931781 2017-10-04 17:10:13.474931994 +0530 IST 1968-07-25 00:59:04.525068432 +0530 IST 프로세스가 0번째 종료 코드로 완료되었습니다
package main import ( "fmt" "time" ) func main() { present := time.Now() fmt.Println("Today : ", present.Format("Mon, Jan 2, 2006 at 3:04오후")) someTime := time.Date(2017, time.March, 30 11, 30 55, 123456, time.Local) // time.Equal() 사용하여 시간 비교 sameTime := someTime.Equal(present) fmt.Println("someTime equals to now ? : ", sameTime) //오늘과 이전 사이의 시차 계산 diff := present.Sub(someTime) //차이를 날짜로 변환 days := int(diff.Hours()) / 24) fmt.Printf("30번째 3월 2017 was %d days ago ", days) }
출력:
Today : Wed, Oct 4, 2017 at 5:15오후 someTime equals to now ? : false 30번째 3월 2017 was 188 날 전