首页 > 其他分享 >golang time包和日期函数

golang time包和日期函数

时间:2024-02-20 19:12:24浏览次数:16  
标签:2024 01 函数 fmt timeObj golang time Println

获取当前时间

	// 获取当前时间对象
	timeObj := time.Now()

	/* 获取当前日期 语法一 */

	// 打印当前日期
	fmt.Println(timeObj) // 2024-02-20 17:50:54.085353 +0800 CST m=+0.000323093
	// 当前年
	year := timeObj.Year()
	// 打印当月
	month := timeObj.Month()
	// 打印当前日
	day := timeObj.Day()
	// 打印当前小时
	hour := timeObj.Hour()
	// 打印当前分
	minute := timeObj.Minute()
	// 打印当前秒
	second := timeObj.Second()

	// 按格式打印日期 // 2024-2-20 17:57:59
	fmt.Printf("%d-%d-%d %d:%d:%d \n", year, month, day, hour, minute, second)
	// 标注日期格式 个位补0输出  2024-02-20 17:59:08
	// %02d中的2表示宽度,整数不够2列就补上0
	fmt.Printf("%d-%02d-%02d %02d:%02d:%02d \n", year, month, day, hour, minute, second)

	/* 获取当前日期 语法二

	go中格式化时间模板不是场景的 y-m-d...
	使用Go的诞生时间 2006年1月2号15点04分
	*/

	// 通过 format格式化输出 24小时制
	// 2024-02-20 18:09:44
	fmt.Println(timeObj.Format("2006-01-02 15:04:05"))
	// 2024/02/20 18:09:44
	fmt.Println(timeObj.Format("2006/01/02 15:04:05"))
	// 18:09:44 2024/02/20
	fmt.Println(timeObj.Format("15:04:05 2006/01/02 "))

	// 通过 format格式化输出 12小时制
	// 2024/02/20 06:09:44
	fmt.Printf(timeObj.Format("2006-01-02 03:04:05"))

获取时间戳及和日期互相转化

	/* 获取当前时间戳 */
	fmt.Println(timeObj.Unix())

	/* 获取纳秒时间戳 */
	fmt.Println(timeObj.UnixNano())

	/* 时间戳转换成日期字符串 */
	// 获取当前时间戳
	unixTime := timeObj.Unix()
	// 通过time获取当前时间戳的时间对象
	obj := time.Unix(int64(unixTime), 0) // Param:秒级的int64的时间戳 , 纳秒的int64时间戳,不用则传0
	// 格式化输出
	fmt.Println(obj.Format("2006-01-02 15:04:05"))

	/* 日期字符串转换为时间戳 */
	nowTimeStr := "2024-01-01 01:01:01"
	// Param:要格式化的时间模板,当前时间字符串,时区(time.Local表示本地时区)
	tObj, _ := time.ParseInLocation("2006-01-02 15:04:05", nowTimeStr, time.Local)
	fmt.Println(tObj)        //  时间对象, 2024-01-01 01:01:01 +0800 CST
	fmt.Println(tObj.Unix()) // 通过时间对象 转换 1704042061

时间间隔

image-20240220183054666

/* 源码定义支持的调用 */
const (
	Nanosecond  Duration = 1
	Microsecond          = 1000 * Nanosecond
	Millisecond          = 1000 * Microsecond
	Second               = 1000 * Millisecond
	Minute               = 60 * Second
	Hour                 = 60 * Minute
)
func main() {
	fmt.Println(time.Second)   // 1s
	fmt.Println(time.Hour * 3) // 3h0m0s

}

时间操作

func main() {

	timeObj := time.Now()

	// 当前时间增加1小时
	fmt.Println(timeObj.Add(time.Hour))

	// 求两个时间对象之间的差值
	// 获取指定时间的时间对象  2024-01-01 00:00:00 +0000 UTC
	t1 := time.Date(2024, time.January, 1, 0, 0, 0, 0, time.UTC)
	t2 := time.Date(2024, time.January, 2, 0, 0, 0, 0, time.UTC)
	duration1 := t2.Sub(t1) // t2 -t1的间隔  24h0m0s
	fmt.Println(duration1)
	fmt.Println(timeObj.Sub(t2)) // 当前时间-t2的间隔  1186h43m6.808475s

	// 判断两个时间,相等则是true,否则false
	t3 := time.Date(2024, time.January, 1, 0, 0, 0, 0, time.UTC)
	t4 := time.Date(2024, time.January, 1, 12, 0, 0, 0, time.UTC)

	isEqual := t4.Equal(t3)
	fmt.Println(isEqual)

	// 判断时间是否在对于时间之前,是的话true 否则 false

	t5 := time.Date(2024, time.January, 1, 0, 0, 0, 0, time.UTC)
	t6 := time.Date(2024, time.January, 2, 12, 0, 0, 0, time.UTC)
	isBefore := t5.Before(t6)
	fmt.Println(isBefore)

	// 判断时间是否在对于时间之后,是的话true 否则 false
	isAfter := t6.After(t5)
	fmt.Println(isAfter)

}

golang定时器

import (
	"fmt"
	"time"
)

func main() {

	/* 间隔定时器 */
	// 定义一个间隔1秒的定时器
	ticker := time.NewTicker(time.Second)

	num := 0

	// 迭代定时器.C
	for t := range ticker.C {
		fmt.Println(t)
		num++

		if num == 5 {
			// 如果执行测试=5,使用Stop终止定时器
			ticker.Stop()
		}

	}

}

标签:2024,01,函数,fmt,timeObj,golang,time,Println
From: https://www.cnblogs.com/Mickey-7/p/18023843

相关文章

  • MySQL——数据处理函数
    MySQL——数据处理函数数据处理函数又被称为单行处理函数,单行处理函数的特点:一个输入对应一个输出.语法格式:select单行处理函数(字段名)from表名;常见单行处理函数转换小写:lower()转换大写:upper()取子串:substr(),语法为substr(字段名,头,尾)。注意:起始下标是从1开......
  • 【C++】判断回文字符串。回文指的是顺读和逆读都一样的字符串。例如,“tot”和“otto”
    //判断字符串是否是回文字符串(考虑大小写,空格和标点符号)boolpalindrome1(string&str){stringret;for(auto&c:str){if(isalpha(c)){if(isupper(c)){ret.push_back(tolower(c));}else{ret.push_back(c);}......
  • python中的内置函数zip函数
    关于zip()函数,有几点要讲的。首先,官方文档中,它是这样描述的:Makeaniteratorthataggregateselementsfromeachoftheiterables.Returnsaniteratoroftuples,wherethei-thtuplecontainsthei-thelementfromeachoftheargumentsequencesoriterables.The......
  • golang函数
    函数定义/*函数定义关键字funcfunc函数名(参数参数类型)函数返回值的类型*/funcgetInfo(namestring,ageint)string{ returnname}//函数返回多个返回值:则返回类型括号包裹(返回值类型,类型..),即时返回两个int,也需要(int,int)funcgetNum(xint,statusboo......
  • Flink 增量窗口聚合函数 ReduceFunction(归约函数)和AggregateFunction(聚合函数)
    Flink增量窗口聚合函数定义了窗口分配器,只是知道了数据属于哪个窗口,可以将数据收集起来了;至于收集起来到底要做什么,其实还完全没有头绪。所以在窗口分配器之后,必须再接上一个定义窗口如何进行计算的操作,这就是所谓的“窗口函数”(windowfunctions)。经窗口分配器处理之后,数据可......
  • windows server 2019/2022安装WSUS更新服务器配置System.Runtime.InteropServices.COM
    现象: 2024-02-1814:41:10Postinstallstarted2024-02-1814:41:10Detectedroleservices:Api,UI,WidDatabase,Services2024-02-1814:41:10Start:LoadSettingsFromXml2024-02-1814:41:10Start:GetConfigValuewithfilename=UpdateServices-Services.xmlit......
  • python不能跳转进入某个函数或模块的一种解决思路
    例如,下图中的get_bucket_mount_root函数可以顺利import进来,但是按ctrl键不能跳转进入这个函数: 一个解决思路是,在vscode终端中,打开python解释器,import上图中的hatbc库,然后用hatbc.__file__命令查找该库的__init__.py文件的路径,按住ctrl键,点击这个路径,即可跳转进入这个__init__.......
  • golang随机数源码分析及应用
    引言大家刚开始使用随机数的时候可能会这样写,但是他会产生一个问题,这是什么问题呢funcmain(){ fori:=0;i<10;i++{ rand.Seed(time.Now().Unix()) fmt.Println(rand.Intn(100)) }}发现打印出来的结果都是相同的让我们看看用代码分析为什么产生这个问题首......
  • 关于vue3的h函数
    h(ElInput,{class:'w200ml8',placeholder:'关键字搜索',clearable:true,modelValue:formData.url_pattern,'onUpdate:modelValue':(val:string)=&......
  • golang数组&切片&map
    数组数组声明funcmain(){ /*语法一*///数组名字[数组长度]数组类型 //声明一个数组长度为3类型是int会初始化为int类型的零值,默认值是[000] //声明数组的时候指定长度是一个常量,数组的不可改变,超出长度会报错 vararr[3]int //数组赋值 arr[0]=1......