日期和時間
package main
import (
"fmt"
"time"
)
func main() {
// 獲取當前時間
current := time.Now()
// 格式化字符串輸出
fmt.Println(current.String())
// Format函數格式化輸出
// 不管要格式化什麼時間,"2006-01-02 15:04:05"這幾個數字固定不變
fmt.Println("MM-DD-YYYY: ", current.Format("01-02-2006"))
fmt.Println("hh:mm:ss MM-DD-YYYY:", current.Format("15:04:05 01-02-2006"))
fmt.Println("YYYY-DD-MM hh:mm:ss:", current.Format("2006-01-02 15:04:05"))
// 添加日期
// AddDate的三個參數依次爲年、月、日
currentAddDate := current.AddDate(-1, 1, 0)
fmt.Println(currentAddDate)
// 添加時間
currentAdd := current.Add(10 * time.Minute)
fmt.Println(currentAdd)
// 獲取時間差,利用Sub函數或者利用Add/AddDate函數增長負值
// Date函數參數:年-月-日-時-分-秒-納秒
currentSub := current.Sub(time.Date(2000, 1, 1, 1, 1, 1, 0, time.UTC))
// currentSub :=currentAdd.Sub(current)
fmt.Println(currentSub)
// 從字符串解析時間
str := "2019-06-29T17:17:17.777Z"
layout := "2006-01-02T15:04:05.000Z"
t, err := time.Parse(layout, str)
if err != nil {
fmt.Println(err)
}
fmt.Println(t)
}