Go語言 時間與時間戳的獲取和轉換

2021-08-29 16:05:46 字數 962 閱讀 2003

csdn部落格:

需要 import 「time」 包

//獲取當前時間戳,單位s,列印結果:1491888244

timeunix:=time.now().unix()

//獲取當前時間戳,單位納秒,列印結果:1491888244752784461

timeunixnano:=time.now().unixnano()

//獲取指定時間的時間戳

loc, _ := time.loadlocation("asia/shanghai") //設定時區

timestr, _ := time.parseinlocation("2006-01-02 15:04:05", "2018-07-11 15:07:58", loc) //2006-01-02 15:04:05是轉換的格式,如php的"y-m-d h:i:s","2018-07-11 15:07:58"才是真正要轉換的時間

fmt.println(timestr.unix()) //列印結果:1531292878

需要 import 「time」 包

t := time.now()	//2018-10-22 06:59:39.399239964 +0000 utc

y := t.year() //年

m := t.month() //月

d := t.day() //日

h := t.hour() //小時

i := t.minute() //分鐘

s := t.second() //秒

fmt.println(y, m, d, h, i, s) //2018 july 11 15 24 59

js獲取時間戳 時間戳轉時間

1 var timestamp date.parse new date 精準到秒2 var timestamp new date valueof 精準到毫秒3.var timestamp new date gettime 精準到毫秒1 var a new date tolocaledatestrin...

Android 獲取時間戳 和時間戳轉日期

獲取系統時間戳 public string gettime 獲取系統時間 long currenttime system.currenttimemillis dateformat formatter new dateformat yyyy年 mm月dd日 hh時mm分ss秒 date date ne...

go語言時間型別和時間戳

獲取當地時間fmt.println time.now fmt.println time.now local fmt.println time.now utc fmt.println time.now location 執行結果 2018 11 2111 50 39.540473 0800 cst m...