Go 字符串时间等相互转化以及时间加减等操作

时间:2025-03-01 20:57:28

1.获取当前时间的第二天零点时间,以及获取当前的23:59:59 的时间戳和date类型:

package main

import (
	"fmt"
	"time"
)

func main(){
	timeStr := ().Format("2006-01-02")
	(timeStr)
	//使用Parse 默认获取为UTC时区 需要获取本地时区 所以使用ParseInLocation
	t2, _ := ("2006-01-02", timeStr, )
	//获取当前时间的第二天零点时间
	tomorrowFirstMoment2 := (0, 0, 1)
	(tomorrowFirstMoment2)

	//获取时间戳
	sjc := ()
	//必须先将时间戳转为才能与相乘
	d := (sjc)*
	now := ().Unix()
	//计算距离当前时间还有多少纳秒
	(int64((now*1000000000)))

	var nowTime = ()
	//获取当前时间的第二天零点时间
	var tomorrowFirstMoment = ((), (), ()+1, 0, 0, 0, 0, ())
	(tomorrowFirstMoment)
	//计算距离当前时间还有多少纳秒
	var d2 = (nowTime)
	(int64(d2))

	//获取当天的23:59:59
	todayyLastMoment, _ := ("2006-01-02 15:04:05", timeStr+" 23:59:59", )
	(todayyLastMoment)
	//计算距离当前时间还有多少纳秒
	var d3 = (nowTime)
	//直接用print的话就不用强转为int64
	print(d3)

}

结果:

2022-04-19
2022-04-20 00:00:00 +0800 CST
49780000000000
2022-04-20 00:00:00 +0800 CST
49779126196000
2022-04-19 23:59:59 +0800 CST
49778126196000
Process finished with exit code 0

从结果可以看出第二天零点和今晚的23:59:59与当前时间相减结果是一样的,不应该有1秒之差吗?

时间解析容易踩坑的地方:

转化时间使用的是UTC时间,所以会自动加8小时,需要改为 ()使用的是cst时间,输出时间正确:

package main

import (
	"fmt"
	"time"
)

func main(){
	t := ().Unix()
	d := (t, 0).Format("2006-01-02 15:04:05")
	tt, _ := ("2006-01-02 15:04:05", d)
	println("===========================")
	println("当前时间戳:", t)
	println("Parse从日期得到时间戳:", ())  //多出了8小时
	tt3, _ := ("2006-01-02 15:04:05", d, )
	println("ParseInLocation从日期得到时间戳:", ()) //时间并没有增加8小时

	now := ()
	("当前时间:", now)
	startDate := "2022-06-01 10:00:00"
	startTime1, _ := ("2006-01-02 15:04:05", startDate)
	("startTime1时间:", startTime1)

	if (startTime1)  {		//由于startTime1用Parse解析出来会增加8小时,所以为false
		println("(startTime1)")
	}
	startTime2, _ := ("2006-01-02 15:04:05", startDate, )
	("startTime2时间:", startTime2)

	if (startTime2)  {
		println("(startTime2)")
	}
}

结果:

===========================
当前时间戳: 1654070070
Parse从日期得到时间戳: 1654098870
ParseInLocation从日期得到时间戳: 1654070070
当前时间: 2022-06-01 15:54:30.526927 +0800 CST m=+0.000301414
startTime1时间: 2022-06-01 10:00:00 +0000 UTC
startTime2时间: 2022-06-01 10:00:00 +0800 CST
(startTime2)

常见时间格式转化:

package main

import (
	"fmt"
	"time"
)

func main(){

	t := int64(1650344950)      //外部传入的时间戳(秒为单位),必须为int64类型
	t1 := "2022-04-19 10:22:30" //外部传入的时间字符串
	timeNow := ()

	//时间转换的模板,golang里面只能是 "2006-01-02 15:04:05" (go的诞生时间)
	timeTemplate1 := "2006-01-02 15:04:05" //常规类型
	timeTemplate2 := "2006/01/02 15:04:05" //其他类型
	timeTemplate3 := "2006-01-02"          //其他类型
	timeTemplate4 := "15:04:05"            //其他类型
	timeTemplate5 := "15"            //获取 当前时间的 时
	timeTemplate6 := "04"            //获取 当前时间的 分
	timeTemplate7 := "05"            //获取 当前时间的 秒

	// ======= 将时间戳格式化为日期字符串 =======
	((t, 0).Format(timeTemplate1)) //输出:2022-04-19 13:09:10
	((t, 0).Format(timeTemplate2)) //输出:2022/04/19 13:09:10
	((t, 0).Format(timeTemplate3)) //输出:2022-04-19
	((t, 0).Format(timeTemplate4)) //输出:13:09:10
	((t, 0).Format(timeTemplate5)) //输出:13
	((t, 0).Format(timeTemplate6)) //输出:09
	((t, 0).Format(timeTemplate7)) //输出:10

	// ======= 将时间字符串转换为时间戳 =======
	stamp, _ := (timeTemplate1, t1, ) //使用parseInLocation将字符串格式化返回本地时区时间
	(stamp)  //2019-01-08 13:50:30 +0800 CST
	((timeTemplate1)) //输出:2022-04-19 10:22:30
	((timeTemplate2)) //输出:2022/04/19 10:22:30
	((timeTemplate3)) //输出:2022-04-19
	((timeTemplate4)) //输出:10:22:30
	((timeTemplate5)) //输出:10
	((timeTemplate6)) //输出:22
	((timeTemplate7)) //输出:30
	(())  //输出:1546926630


	(timeNow)  //2022-04-19 10:25:33.674693 +0800 CST m=+0.000083549
	((timeTemplate1)) //输出:2022-04-19 10:25:33
	((timeTemplate2)) //输出:2022/04/19 10:25:33
	((timeTemplate3)) //输出:2022-04-19
	((timeTemplate4)) //输出:10:25:33
	((timeTemplate5)) //输出:10
	((timeTemplate6)) //输出:25
	((timeTemplate7)) //输出:33


}

结果:

2022-04-19 13:09:10
2022/04/19 13:09:10
2022-04-19
13:09:10
13
09
10
2022-04-19 10:22:30 +0800 CST
2022-04-19 10:22:30
2022/04/19 10:22:30
2022-04-19
10:22:30
10
22
30
1650334950
2022-04-19 10:31:13.363526 +0800 CST m=+0.000091492
2022-04-19 10:31:13
2022/04/19 10:31:13
2022-04-19
10:31:13
10
31
13