- 时间类型在实际开发中经常使用,它代表了真实世界的时间
- 在围棋语言中,时间类型不是关键字,而是时间包下的NicholasTse结构
- 时间类型默认显示为UTC,因此通常需要将时间类型转换为字符串,并以我们熟悉的格式显示
- 当宣布Time时,默认时间毫无意义
包干管
进口(
"fmt和#34;
"时间";
)
//以下代码块中省略了上述内容
funcmain(){
时间到了。Time
fmt。Println(T)//输出:0001-01-0100:00:00+0000UTC
}
- 您可以通过时间包下的now()函数获取操作系统的当前时间
+0800CST指北京时间东八区
funcmain(){
时间。现在()
fmt。Println(T)//输出:2020-12-3021:02:03.0912064+0800CSTM=+0.017988201
}
- 还可以创建带有时间戳的时间类型变量(与1970年1月1日相差纳秒)
funcmain(){
/*
1秒=1000毫秒(毫秒)
1秒=1000000个细节(美国)
1秒=1000纳秒(NS)
*/
时间。现在()
t1:=时间。UNIX(0,t.unixnano())//根据时间戳创建时间,第二个值[0,99999999]是有效的
fmt。Println(t.string())//输出:2021-01-0115:19:43.1850814+0800CSTM=+0.017950101
fmt。Println(T1)//输出:2021-01-0115:19:43.1850814+0800CST
}
- 根据你的需求创造时间
funcmain(){
时间。日期(当地时间2020年5月23日7月8日9月0日)
fmt。Println(T)//输出:2020-05-2307:08:09+0800CST
}
时间包下提供了大量函数或方法来获取特定的时间项
funcmain(){
时间。现在()
fmt。Println(t)
fmt.//年份(打印)
fmt。Println(int(t.month())//month
fmt。Println(t.day())//day
fmt。Println(t.date())//三个参数:年、月和日
fmt。Println(t.hour())//hours
fmt。Println(t.minute())//分钟
fmt。Println(t.second())//秒
fmt。Println(t.clock())//三个参数:小时、分钟和秒
fmt。Println(t.纳秒())//纳秒
fmt。Println(t.unix())//第二个区别
fmt。Println(t.unixnano())//纳秒差
}
- 时间转换为string
参数必须是thistime,即go语言的出现时间
funcmain(){
时间。现在()
//这次参数必须是任何格式
s:=t.格式(";2020-05-2315:04:05";,)
fmt。普林顿(s)
}
- string皈依了时间
funcmain(){
s:=";2022-02-0415:04:05"
t、时间。解析(";2022-02-0422:02:04";s)
fmt。Println(t,err)
}
最新评论