本博客转自: https://blog.csdn.net/mirage003/article/details/86073046

package main

import (
"log"
"time"
) func main() {
t := int64(1546926630) //外部传入的时间戳(秒为单位),必须为int64类型
t1 := "2019-01-08 13:50:30" //外部传入的时间字符串 //时间转换的模板,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" //其他类型 // ======= 将时间戳格式化为日期字符串 =======
log.Println(time.Unix(t, 0).Format(timeTemplate1)) //输出:2019-01-08 13:50:30
log.Println(time.Unix(t, 0).Format(timeTemplate2)) //输出:2019/01/08 13:50:30
log.Println(time.Unix(t, 0).Format(timeTemplate3)) //输出:2019-01-08
log.Println(time.Unix(t, 0).Format(timeTemplate4)) //输出:13:50:30 // ======= 将时间字符串转换为时间戳 =======
stamp, _ := time.ParseInLocation(timeTemplate1, t1, time.Local) //使用parseInLocation将字符串格式化返回本地时区时间
log.Println(stamp.Unix()) //输出:1546926630
}
package main

import (
"log"
"time"
)
func main(){
// 获取每天的零点时间戳, 一个小时的时间戳是3600
timeStr := time.Now().Format("2006-01-02")
t, _ := time.ParseInLocation("2006-01-02", timeStr, time.Local)
timeUnix := t.Unix()
}
package main

import (
"fmt"
"strconv"
"time"
) func main() {
t := time.Now()
fmt.Println(t) fmt.Println(t.UTC().Format(time.UnixDate)) fmt.Println(t.Unix()) timestamp := strconv.FormatInt(t.UTC().UnixNano(), 10)
fmt.Println(timestamp)
timestamp = timestamp[:10]
fmt.Println(timestamp)
} //输出:
//2019-09-02 19:17:58.2508394 +0800 CST m=+0.001994001
//Mon Sep 2 11:17:58 UTC 2019
//1567423078
//1567423078250839400
//1567423078
package main

import (
"fmt"
"strconv"
"time"
) func main() {
const longForm = "Jan 2, 2006 at 3:04pm (MST)"
t, _ := time.Parse(longForm, "Jun 21, 2017 at 0:00am (PST)")
fmt.Println(t) const shortForm = "2006-Jan-02"
t, _ = time.Parse(shortForm, "2017-Jun-21")
fmt.Println(t) t, _ = time.Parse("01/02/2006", "06/21/2017")
fmt.Println(t)
fmt.Println(t.Unix()) i, err := strconv.ParseInt("1498003200", 10, 64)
if err != nil {
panic(err)
}
tm := time.Unix(i, 0)
fmt.Println(tm) var timestamp int64 = 1498003200
tm2 := time.Unix(timestamp, 0)
fmt.Println(tm2.Format("2006-01-02 03:04:05 PM"))
fmt.Println(tm2.Format("02/01/2006 15:04:05 PM"))
} //输出
//2017-06-21 00:00:00 +0000 PST
//2017-06-21 00:00:00 +0000 UTC
//2017-06-21 00:00:00 +0000 UTC
//1498003200
//2017-06-21 08:00:00 +0800 CST
//2017-06-21 08:00:00 AM
//21/06/2017 08:00:00 AM
time常用方法

After(u Time) bool
时间类型比较,是否在Time之后 Before(u Time) bool
时间类型比较,是否在Time之前 Equal(u Time) bool
比较两个时间是否相等 IsZero() bool
判断时间是否为零值,如果sec和nsec两个属性都是0的话,则该时间类型为0 Date() (year int, month Month, day int)
返回年月日,三个参数 Year() int
返回年份 Month() Month
返回月份.是Month类型 Day() int
返回多少号 Weekday() Weekday
返回星期几,是Weekday类型 ISOWeek() (year, week int)
返回年份,和该填是在这年的第几周. Clock() (hour, min, sec int)
返回小时,分钟,秒 Hour() int
返回小时 Minute() int
返回分钟 Second() int
返回秒数 Nanosecond() int
返回纳秒

计算时间差

package main

import (
"fmt"
"strings"
"time"
) func main() {
// Add 时间相加
now := time.Now()
// ParseDuration parses a duration string.
// A duration string is a possibly signed sequence of decimal numbers,
// each with optional fraction and a unit suffix,
// such as "300ms", "-1.5h" or "2h45m".
// Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
// 10分钟前
m, _ := time.ParseDuration("-1m")
m1 := now.Add(m)
fmt.Println(m1) // 8个小时前
h, _ := time.ParseDuration("-1h")
h1 := now.Add(8 * h)
fmt.Println(h1) // 一天前
d, _ := time.ParseDuration("-24h")
d1 := now.Add(d)
fmt.Println(d1) printSplit(50) // 10分钟后
mm, _ := time.ParseDuration("1m")
mm1 := now.Add(mm)
fmt.Println(mm1) // 8小时后
hh, _ := time.ParseDuration("1h")
hh1 := now.Add(hh)
fmt.Println(hh1) // 一天后
dd, _ := time.ParseDuration("24h")
dd1 := now.Add(dd)
fmt.Println(dd1) printSplit(50) // Sub 计算两个时间差
subM := now.Sub(m1)
fmt.Println(subM.Minutes(), "分钟") sumH := now.Sub(h1)
fmt.Println(sumH.Hours(), "小时") sumD := now.Sub(d1)
fmt.Printf("%v 天\n", sumD.Hours()/24) } func printSplit(count int) {
fmt.Println(strings.Repeat("#", count))
}

golang的time包:时间字符串和时间戳的相互转换的更多相关文章

  1. 用shell将时间字符串与时间戳互转

    date的详细用户可以参考下面的 http://www.cnblogs.com/xd502djj/archive/2010/12/29/1919478.html date 的具体用法可以查看另外一篇博 ...

  2. Python 之 时间字符串、时间戳、时间差、任意时间字符串转换时间对象

    1. 时间字符串 --> 时间戳 1) time 模块 timestring = '2016-12-21 10:22:56' print time.mktime(time.strptime(ti ...

  3. 微坑---微信小程序ios上时间字符串转换为时间戳时,在开发工具上和安卓手机上运行成功

    给定一个时间字符串  var time="2017-02-27 16:42:53" js有三种转换为时间戳的方法:1.var timestamp = Date.parse(time ...

  4. JS 时间字符串与时间戳之间的转换

    1.当前时间换时间戳 var timestamp = parseInt(new Date().getTime()/1000); // 当前时间戳 document.write(timestamp); ...

  5. 微信小程序ios上时间字符串转换为时间戳时会报错,在开发工具上和安卓手机上运行成功

    给定一个时间字符串  var time="2017-02-27 16:42:53" js有三种转换为时间戳的方法: 1.var timestamp = Date.parse(tim ...

  6. python 时间字符串和时间戳之间的转换

    https://blog.csdn.net/qq_37193537/article/details/78987949   1.将字符串的时间转换为时间戳    方法:        a = " ...

  7. IOS 时间字符串转换时间戳失败问题

    链接:https://pan.baidu.com/s/1nw6VWoD 密码:1peh 有时候获取到的时间带有毫秒数或者是(2018-2-6 11:11:11)格式的(别说你没遇到过,也别什么都让后台 ...

  8. 【js Date】时间字符串、时间戳转换成今天,明天,本月等文字日期

    作为前端开发攻城师,难免对时间进行各种计算和格式转换,一个js的Date对象统统可以搞定.下例是将一个具体的时间转换成今天.明天.几天之内.本月等文字描述的日期的工具函数,也可以基于它扩展,多应用于网 ...

  9. js时间字符串转时间戳

    字符串形如:2016-06-20 10:41 转换为时间戳: var date = "2016-06-20 10:41"; date = date.substring(,); da ...

随机推荐

  1. [HTML5] Add Semantic Styling to the Current Page of a Navigation Item with aria-current

    In this lesson, we are going to use aria-current to give a screen reader user more context about wha ...

  2. E:only-child

    E:only-child 语法: E:only-child { sRules } 说明: 匹配父元素仅有的一个子元素E.大理石机械构件维修 要使该属性生效,E元素必须是某个元素的子元素,E的父元素最高 ...

  3. 04_实时监控本机内存和硬盘剩余空间,剩余内存小于 500M、根分区剩余空间小于 1000M 时,发送报警邮件给root 管理员.

    #!/bin/bash#提取根分区剩余空间disk_size=$(df -h / | awk '/\//{print $4}')#提取内存剩余空间disk_size=$(df -h / | awk ' ...

  4. springboot o.a.tomcat.util.scan.StandardJarScanner : Failed to scan [file:/D:/apache-maven-3.0.5[系统找不到指定路径]

    报错信息: 2019-11-04 11:05:52.404 WARN 4512 --- [ main] o.a.tomcat.util.scan.StandardJarScanner : Failed ...

  5. 2019牛客暑期多校训练营(第六场)E 构造、原图是补图的同构图

    https://ac.nowcoder.com/acm/contest/886#question 题意  问是否存在某个n个点的无向图G是其补图H的同构图,若存在输出G的邻接矩阵以及H关于G的映射. ...

  6. 百度翻译api初使用(很久没写python了,写几行玩玩)

    调用free api做做简易的翻译 这个是百度翻译api文档 http://api.fanyi.baidu.com/api/trans/product/apidoc 照着百度api给的文档向web服务 ...

  7. c 语言延时函数

    /*--- 等待x毫秒 ---*/ int sleep(unsigned long x) { clock_t c1 = clock(), c2; do { ) /* 错误 */ ; } while ( ...

  8. java微服务简介与实战

    今年做了一段时间的可见光.ceph存储,后端开发微服务项目,在这记录点东西,也方便大家借鉴查找. springboot的项目实例:https://github.com/ityouknow/spring ...

  9. 动态规划——区间DP,计数类DP,数位统计DP

    本博客部分内容参考:<算法竞赛进阶指南> 一.区间DP 划重点: 以前所学过的线性DP一般从初始状态开始,沿着阶段的扩张向某个方向递推,直至计算出目标状态. 区间DP也属于线性DP的一种, ...

  10. 关系型数据库的树形结构查询(Oracle、Postgres)

    Oracle : start with… connect by 的用法.语法:select * from table [where 条件1] connect by[条件2] start with[条件 ...