go 日期時間戳之間的轉換(time包)

2021-10-10 04:47:47 字數 1848 閱讀 1574

// 獲取當前日期 物件 型別為 time.time

now = time.now()

fmt.println(now)

// 獲取當前日期 物件 的 年 月 日 時 分 秒 時間戳

fmt.println(now.year())

fmt.println(now.month())

fmt.println(now.day())

fmt.println(now.hour())

fmt.println(now.minute())

fmt.println(now.second())

fmt.println(now.unix())

// 獲取當前日期物件格式化

fmt.println(now.format("2006-01-02 15:04:05"))

// 指定時間戳 轉 日期 物件 型別為 time.time

fmt.println("指定時間戳 轉 日期", time.unix(1446425371, 0))

// 指定日期 轉 時間戳

setdate := "2015-03-02 12:02:03"

dateformate := "2006-01-02 15:04:05"

loc, _ := time.loadlocation("local") //重要:獲取時區

timeobj, err := time.parseinlocation(dateformate, setdate, loc) //指定日期 轉 當地 日期物件 型別為 time.time

if err != nil

fmt.printf("%t\n", timeobj)

fmt.println(timeobj)

fmt.println(timeobj.unix())

fmt.println(timeobj.year())

fmt.println(timeobj.month())

//指定日期格式化

fmt.println(timeobj.format("2006-01-02"))

now = time.now()

fmt.println(now)

//時間加 一天

fmt.println(now.add(24 * time.hour))

//時間減 一天

fmt.println(now.add(-24 * time.hour))

// utc 轉 cst 世界日期物件 轉 中國 日期物件

now = now.utc()

fmt.println(now)

// cst 轉 utc 中國 日期物件 轉 界日期物件

datef := now.format("2006-01-02 15:04:05")

//loc, _ := time.loadlocation("local")

fmt.println(time.parseinlocation(dateformate, datef, loc))

// sub 兩個時間相減

//loc, _ := time.loadlocation("local") //重要:獲取時區

nextyear, err1 := time.parseinlocation(dateformate, setdate, loc) //指定日期 轉 當地 日期物件 型別為 time.time

if err1 != nil

now = time.now()

d1 := nextyear.sub(now)

fmt.println(d1)

時間戳轉換日期 日期轉換時間戳 時間戳轉換星期

時間戳轉換日期 string date orderinfo.getchangetime replace null dateformat sdf new dateformat mm dd string monthdate sdf.format new date long.parselong date ...

Python時間,日期,時間戳之間轉換

1.將字串的時間轉換為時間戳 方法 a 2013 10 10 23 40 00 將其轉換為時間陣列 importtime timearray time.strptime a,y m d h m s 轉換為時間戳 timestamp int time.mktime timearray timestam...

Python時間,日期,時間戳之間轉換

方法 a 2013 10 10 23 40 00 將其轉換為時間陣列 importtime timearray time.strptime a,y m d h m s 轉換為時間戳 timestamp int time.mktime timearray timestamp 1381419600 2....