這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
擷取本地時間
// get current timestamp currentTime := time.Now().Local() //print time fmt.Println(currentTime)
指定格式的日期字元類型
// get current timestamp currentTime := time.Now().Local()//format Time, string type newFormat := currentTime.Format("2006-01-02 15:04:05.000") fmt.Println(newFormat)
構造指定時間類型
//build Time 2016-02-17 23:59:59.999, DateTime type myTime := time.Date(0162, time.February, 17, 23, 59, 59, 999, time.UTC) //output the myTime fmt.Println("MyTime:", myTime.Format("2006-01-02 15:04:05.000"))
完整代碼:
package mainimport ( "fmt" "time")func FormatTime() { // get current timestamp currentTime := time.Now().Local() //print time fmt.Println(currentTime) //format Time, string type newFormat := currentTime.Format("2006-01-02 15:04:05.000") fmt.Println(newFormat) //build Time 2016-02-17 23:59:59.999, DateTime type myTime := time.Date(0162, time.February, 17, 23, 59, 59, 999, time.UTC) //output the myTime fmt.Println("MyTime:", myTime.Format("2006-01-02 15:04:05.000")) //current milliseconds fmt.Println("milliseconds:", time.Now().UnixNano()/int64(time.Millisecond)) //TODO Changing time layout(form)}func main(){FormatTime()}
歡迎加入我的公眾號