golang 日期資訊和時間戳

2021-09-25 17:28:14 字數 1027 閱讀 7289

1、獲取當前時間

currenttime:=time.now() //獲取當前時間,型別是go的時間型別time

t1:=time.now().year()//年

t2:=time.now().month()   //月

t3:=time.now().day() //日

t4:=time.now().hour()//小時

t5:=time.now().minute()  //分鐘

t6:=time.now().second()  //秒

t7:=time.now().nanosecond()  //納秒

currenttimedata:=time.date(t1,t2,t3,t4,t5,t6,t7,time.local) //獲取當前時間,返回當前時間time 

fmt.println(currenttime)   //列印結果:2017-04-11 12:52:52.794351777 +0800 cst

fmt.println(t1,t2,t3,t4,t5,t6) //列印結果:2017 april 11 12 52 52

fmt.println(currenttimedata)//列印結果:2017-04-11 12:52:52.794411287 +0800 cst

2、時間戳

時間戳有10位、13位、還有好長位數的。

10位數的時間戳是以 秒 為單位;

13位數的時間戳是以 毫秒 為單位;

19位數的時間戳是以 納秒 為單位;

golang中可以這樣寫:

package main

import (

"time"

"fmt"

)func main()

輸出結果為:

時間戳(秒):1530027865;

時間戳(納秒):1530027865231834600;

時間戳(毫秒):1530027865231;

時間戳(納秒轉換為秒):1530027865;

Golang獲取時間戳和時間操作

time.now unix 時間戳 秒 time.now unixnano 時間戳 納秒 time.now unixnano 1e6 時間戳 毫秒 time.now unixnano 1e9 時間戳 納秒轉換為秒 注 1秒 1000毫秒 1毫秒 1000微秒 1微秒 1000納秒 1e6是指數表達形...

日期和時間戳互轉

1 日期轉為時間戳 vardate newdate 2014 04 23 18 55 49 123 有三種方式獲取 vartime1 date.gettime vartime2 date.valueof vartime3 date.parse date console.log time1 13982...

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

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