Golang获取年月日时间字符串和时间戳
1、获取字符串时间
//获取年、月、日、时、分、秒
getYear := time.Now().Year() //获取年
getMonth := time.Now().Format("01") //获取月
getDay := time.Now().Day() 获取日
或者
getYear := time.Now().Format("2006") //获取年
getMonth := time.Now().Format("01") //获取月
getDay := time.Now().Format("02") 获取日
hour := time.Now().Format("15")
min := time.Now().Format("04")
second := time.Now().Format("05")fmt.Println("year:", year, "month:", month, "day:", day)
fmt.Println("hour:", hour, "min:", min, "second:", second)
//输出:year: 2021 month: 04 day: 23
// hour: 15 min: 16 second: 53
//获取日期各种格式
todaystr1 := time.Now().Format("2006-01-02 15:04:05")
fmt.Println("todaystr1:", todaystr1)todaystr2 := time.Now().Format("2006/01/02 15:04:05")
fmt.Println("todaystr2:", todaystr2)todaystr3 := time.Now().Format("2006-01-02")
fmt.Println("todaystr3:", todaystr3)todaystr4 := time.Now().Format("15:04:05")
fmt.Println("todaystr4:", todaystr4)//输出:todaystr1: 2021-04-23 15:32:38todaystr2: 2021/04/23 15:32:38todaystr3: 2021-04-23todaystr4: 15:32:38
2、获取时间戳
//时间转换的模板,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" //其他类型//当前日期(年-月-日)的时间戳
todaystr := time.Now().Format("2006-01-02")
//使用parseInLocation将字符串格式化返回本地时区时间
todayint, _ := time.ParseInLocation(timeTemplate3, todaystr, time.Local)
fmt.Println("日期:", todaystr, "的时间戳是:", todayint.Unix())
//输出:日期: 2021-04-23 的时间戳是: 1619107200//当前时间(年-月-日 时:分:秒)的时间戳
todaystr := time.Now().Format("2006-01-02 15:04:05")
todayint, _ := time.ParseInLocation(timeTemplate1 , todaystr, time.Local) /
fmt.Println("日期:", todaystr, "的时间戳是:", todayint.Unix())
//输出:日期: 2021-04-23 15:44:19 的时间戳是: 1619163859
注:获取前一天日期:
currentTime := time.Now()
oldTime1 := currentTime.AddDate(0, 0, -1) //前5天就写-5。
先写到这儿,以后用到再补充。
参考文章:golang的time包:时间字符串和时间戳的相互转换
Golang如何获取当前年份月份日
Golang获取年月日时间字符串和时间戳相关推荐
- golang时间字符串和时间戳转换
1. 获取当前时间字符串和时间戳 package mainimport ("fmt""time" )func main() {now := time.Now() ...
- golang获取当前时间、时间戳和时间字符串及它们之间的相互转换
1.获取当前时间 (1) currentTime:=time.Now() //获取当前时间,类型是Go的时间类型Time (2) t1:=time.Now().Year() //年t2:=time.N ...
- golang的time包:时间字符串和时间戳的相互转换
本博客转自: https://blog.csdn.net/mirage003/article/details/86073046 package mainimport ("log"& ...
- php 字符串转时间戳_php如何将时间字符串转换为时间戳
php将时间字符串转换为时间戳的方法:可以利用内置函数strtotime()来实现.strtotime()函数可以将任何英文文本的日期或时间描述解析为Unix时间戳,若成功则返回时间戳,否则返回fal ...
- oracle的获取年月日时间,获取5年前日期
oracle的获取年月日时间 正常获取时间 select sysdate from dual:-- 当前时间 select to_char(sysdate,'YYYY') from dual; --获 ...
- 时间字符串与时间戳批量转换
原文博客地址:https://blog.csdn.net/s1164548515/article/details/100186773 时间字符串与时间戳批量转换 import pandas as pd ...
- C/C++时间字符串和时间戳的相互转化
用到的函数主要有strftime.strptime .localtime .mktime. 名词解释: 时间戳:是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日 ...
- Scala 将时间字符串转为时间戳
今天需要将Nginx日志中的时间字符串转为时间戳,再加以分析,由于刚学Scala不久,Java也只是一知半解,所以在时间转换的时候浪费了一些时间,故在此备忘,也希望能帮到有需要的同学. 部分日志格式如 ...
- java时间字符串转时间戳
java时间字符串转时间戳 测试类 代码片. @Testpublic void dateToStamp() throws Exception {String str = "2019-03-1 ...
最新文章
- Axure8.0 网页 or App 鼠标滚动效果
- 国立华侨大学校长写给2010届毕业生的话:人生的二和三
- css设置元素继承父元素宽度_CSS设置HTML元素的高度与宽度的各种情况总结
- @你最强资源包来了 MobTech 联合 LiveVideoStack 发起劳动光荣榜
- tcp 接收端优雅的写法_如何更优雅地接收设计反馈
- Oracle数据库在Nokia
- 前端学习(1349):用户的增删改查操作6删除
- 2017年智能家居行业发展脉络解析
- 【算法】多路查找树 B树 B+树
- AngularJS开发指南6:AngularJS表单详解
- iOS 13问题记录
- linux备份系统 硬盘数据,Linux怎么备份系统
- 数百种 Windows 软件的免费替代品列表(转)
- PHP爬虫最全总结2-phpQuery,PHPcrawer,snoopy框架中文介绍
- 嵌入式系统开发笔记25:win10防火墙打开特定端口
- HTML5小游戏之爱心鱼
- 用java打暴雪星际争霸(2)——运行测试机器人
- [推荐](穆穆推荐)如果你现在不成功,请看看你是不是选错了老板
- Windows+GCC得到各种格式的文件正文(office文件,PDF,邮件,html,zip等)
- cocos2dx-精灵的移动跳跃