golang中关于time模块的常用方法-创新互联

在开发的过程中,大家都会使用到golang中的time模块进行一些时间的处理,以及格式化下面我总结了一些time中常用的方法运行就可以使用
package main

import (
	"fmt"
	"time"
)

func main() {//	获取当前时间
	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())
	//  通过指定格式将时间格式转换为指定的时间字符串类型
	format1 := now.Format("2006-01-02 15:04:05")
	format2 := now.Format("2006/01/02 15:04:05")
	format3 := now.Format("2006-01-02")
	format4 := now.Format("2006/01/02")
	format5 := now.Format("15:04:05")
	fmt.Println(format1)
	fmt.Println(format2)
	fmt.Println(format3)
	fmt.Println(format4)
	fmt.Println(format5)
	// 获取秒
	fmt.Println(now.Unix())
	// 获取毫秒
	fmt.Println(now.UnixMilli())
	// 获取微妙
	fmt.Println(now.UnixMicro())
	// 获取纳秒
	fmt.Println(now.UnixNano())

	time1 := time.Unix(now.Unix(), 0).Format("2006-01-02 15:04:05")
	time2 := time.UnixMilli(now.UnixMilli()).Format("2006-01-02 15:04:05")
	time3 := time.UnixMicro(now.UnixMicro()).Format("2006-01-02 15:04:05")
	fmt.Println(time1)
	fmt.Println(time2)
	fmt.Println(time3)

	//	进行时间的添加操作, 负数为减少,整数为添加, 可以是小时  天  秒  分钟  年
	NewTime := now.Add(time.Hour * 1)
	fmt.Println(NewTime)

	// 计算两个时间的差
	SubTime := NewTime.Sub(now)
	fmt.Println(SubTime)

	// 计算当前时间与某个时间的差
	fmt.Println(time.Since(NewTime))

	// 判断当前时间是否在某个时间之前
	date := time.Date(2022, 12, 05, 12, 30, 45, 0, time.UTC)
	fmt.Println(now.Before(date))

	//  判断当前时间是否在某个时间之后
	fmt.Println(now.After(date))
}

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧

创新互联公司主营宜都网站建设的网络公司,主营网站建设方案,重庆APP软件开发,宜都h5微信小程序开发搭建,宜都网站营销推广欢迎宜都等地区企业咨询

网页标题:golang中关于time模块的常用方法-创新互联
分享网址:https://www.cdcxhl.com/article44/phjee.html

成都网站建设公司_创新互联,为您提供移动网站建设电子商务用户体验企业建站外贸建站品牌网站制作

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

外贸网站制作