下面的程序使用解析函数从当前日期和时间获取年、月、日、小时、分钟和秒。Clock函数用于返回时间T指定的一天内的小时、分钟和秒。
示例代码:
package main
import (
"fmt"
"time"
)
func main() {
currentTime := time.Now()
fmt.Println("\n######################################\n")
fmt.Println(currentTime.Format("2006-01-02 15:04:05"))
fmt.Println("\n######################################\n")
timeStampString := currentTime.Format("2006-01-02 15:04:05")
layOut := "2006-01-02 15:04:05"
timeStamp, err := time.Parse(layOut, timeStampString)
if err != nil {
fmt.Println(err)
}
hr, min, sec := timeStamp.Clock()
fmt.Println("Year :", currentTime.Year())
fmt.Println("Month :", currentTime.Month())
fmt.Println("Day :", currentTime.Day())
fmt.Println("Hour :", hr)
fmt.Println("Min :", min)
fmt.Println("Sec :", sec)
fmt.Println("\n######################################\n")
year, month, day := time.Now().Date()
fmt.Println("Year :", year)
fmt.Println("Month :", month)
fmt.Println("Day :", day)
fmt.Println("\n######################################\n")
t := time.Now()
y := t.Year()
mon := t.Month()
d := t.Day()
h := t.Hour()
m := t.Minute()
s := t.Second()
n := t.Nanosecond()
fmt.Println("Year :",y)
fmt.Println("Month :",mon)
fmt.Println("Day :",d)
fmt.Println("Hour :",h)
fmt.Println("Minute :",m)
fmt.Println("Second :",s)
fmt.Println("Nanosec:",n)
}
输出:
######################################
2017-08-27 18:11:54
######################################
Year : 2017
Month : August
Day : 27
Hour : 18
Min : 11
Sec : 54
######################################
Year : 2017
Month : August
Day : 27
######################################
Year : 2017
Month : August
Day : 27
Hour : 18
Minute : 11
Second : 54
Nanosec: 319513100
以下是从当前时间减去指定的日期或时间,与增加时的方法相同,AddDate用于计算日期,Add用于计算时间,只不过减去时用的是负数。
示例代码:
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now()
fmt.Println("Today:", now)
after := now.AddDate(-1, 0, 0)
fmt.Println("Subtract 1 Year:", after)
after = now.AddDate(0, -1, 0)
fmt.Println("Subtract 1 Month:", after)
after = now.AddDate(0, 0, -1)
fmt.Println("Subtract 1 Day:", after)
after = now.AddDate(-2, -2, -5)
fmt.Println("Subtract multiple values:", after)
after = now.Add(-10*time.Minute)
fmt.Println("Subtract 10 Minutes:", after)
after = now.Add(-10*time.Second)
fmt.Println("Subtract 10 Second:", after)
after = now.Add(-10*time.Hour)
fmt.Println("Subtract 10 Hour:", after)
after = now.Add(-10*time.Millisecond)
fmt.Println("Subtract 10 Millisecond:", after)
after = now.Add(-10*time.Microsecond)
fmt.Println("Subtract 10 Microsecond:", after)
after = now.Add(-10*time.Nanosecond)
fmt.Println("Subtract 10 Nanosecond:", after)
}
输出:
Today: 2017-08-27 12:21:17.8379942 +0530 IST
Subtract 1 Year: 2016-08-27 12:21:17.8379942 +0530 IST
Subtract 1 Month: 2017-07-27 12:21:17.8379942 +0530 IST
Subtract 1 Day: 2017-08-26 12:21:17.8379942 +0530 IST
Subtract multiple values: 2015-06-22 12:21:17.8379942 +0530 IST
Subtract 10 Minutes: 2017-08-27 12:11:17.8379942 +0530 IST
Subtract 10 Second: 2017-08-27 12:21:07.8379942 +0530 IST
Subtract 10 Hour: 2017-08-27 02:21:17.8379942 +0530 IST
Subtract 10 Millisecond: 2017-08-27 12:21:17.8279942 +0530 IST
Subtract 10 Microsecond: 2017-08-27 12:21:17.8379842 +0530 IST
Subtract 10 Nanosecond: 2017-08-27 12:21:17.83799419 +0530 IST
以下是在golang中进行常规时间计算的方法。AddDate用于日期计算,Add用于时间计算。
示例代码:
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now()
fmt.Println("\nToday:", now)
after := now.AddDate(1, 0, 0)
fmt.Println("\nAdd 1 Year:", after)
after = now.AddDate(0, 1, 0)
fmt.Println("\nAdd 1 Month:", after)
after = now.AddDate(0, 0, 1)
fmt.Println("\nAdd 1 Day:", after)
after = now.AddDate(2, 2, 5)
fmt.Println("\nAdd multiple values:", after)
after = now.Add(10*time.Minute)
fmt.Println("\nAdd 10 Minutes:", after)
after = now.Add(10*time.Second)
fmt.Println("\nAdd 10 Second:", after)
after = now.Add(10*time.Hour)
fmt.Println("\nAdd 10 Hour:", after)
after = now.Add(10*time.Millisecond)
fmt.Println("\nAdd 10 Millisecond:", after)
after = now.Add(10*time.Microsecond)
fmt.Println("\nAdd 10 Microsecond:", after)
after = now.Add(10*time.Nanosecond)
fmt.Println("\nAdd 10 Nanosecond:", after)
}
输出:
Today: 2017-08-27 11:17:54.1224628 +0530 IST
Add 1 Year: 2018-08-27 11:17:54.1224628 +0530 IST
Add 1 Month: 2017-09-27 11:17:54.1224628 +0530 IST
Add 1 Day: 2017-08-28 11:17:54.1224628 +0530 IST
Add multiple values: 2019-11-01 11:17:54.1224628 +0530 IST
Add 10 Minutes: 2017-08-27 11:27:54.1224628 +0530 IST
Add 10 Second: 2017-08-27 11:18:04.1224628 +0530 IST
Add 10 Hour: 2017-08-27 21:17:54.1224628 +0530 IST
Add 10 Millisecond: 2017-08-27 11:17:54.1324628 +0530 IST
Add 10 Microsecond: 2017-08-27 11:17:54.1224728 +0530 IST
Add 10 Nanosecond: 2017-08-27 11:17:54.12246281 +0530 IST
以下是计算两个时间之间之差的方法,具体到天,时,分,秒。
示例代码:
package main
import (
"fmt"
"time"
)
func main() {
loc, _ := time.LoadLocation("UTC")
now := time.Now().In(loc)
fmt.Println("\nToday : ", loc, " Time : ", now)
pastDate := time.Date(2015, time.May, 21, 23, 10, 52, 211, time.UTC)
fmt.Println("\n\nPast : ", loc, " Time : ", pastDate) //
fmt.Printf("###############################################################\n")
diff := now.Sub(pastDate)
hrs := int(diff.Hours())
fmt.Printf("Diffrence in Hours : %d Hours\n", hrs)
mins := int(diff.Minutes())
fmt.Printf("Diffrence in Minutes : %d Minutes\n", mins)
second := int(diff.Seconds())
fmt.Printf("Diffrence in Seconds : %d Seconds\n", second)
days := int(diff.Hours() / 24)
fmt.Printf("Diffrence in days : %d days\n", days)
fmt.Printf("###############################################################\n\n\n")
futureDate := time.Date(2019, time.May, 21, 23, 10, 52, 211, time.UTC)
fmt.Println("Future : ", loc, " Time : ", futureDate) //
fmt.Printf("###############################################################\n")
diff = futureDate.Sub(now)
hrs = int(diff.Hours())
fmt.Printf("Diffrence in Hours : %d Hours\n", hrs)
mins = int(diff.Minutes())
fmt.Printf("Diffrence in Minutes : %d Minutes\n", mins)
second = int(diff.Seconds())
fmt.Printf("Diffrence in Seconds : %d Seconds\n", second)
days = int(diff.Hours() / 24)
fmt.Printf("Diffrence in days : %d days\n", days)
}
输出:
Today : UTC Time : 2017-08-27 05:15:53.7106215 +0000 UTC
Past : UTC Time : 2015-05-21 23:10:52.000000211 +0000 UTC
###############################################################
Diffrence in Hours : 19878 Hours
Diffrence in Minutes : 1192685 Minutes
Diffrence in Seconds : 71561101 Seconds
Diffrence in days : 828 days
###############################################################
Future : UTC Time : 2019-05-21 23:10:52.000000211 +0000 UTC
###############################################################
Diffrence in Hours : 15185 Hours
Diffrence in Minutes : 911154 Minutes
Diffrence in Seconds : 54669298 Seconds
Diffrence in days : 632 days