本博客转自: 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. django加载本地html

    django加载本地html from django.shortcuts import render from django.http import HttpResponse from django. ...

  2. Tinkoff Internship Warmup Round 2018 and Codeforces Round #475 (Div. 1) 963B 964D B Destruction of a Tree

    题 OvO http://codeforces.com/contest/963/problem/B CF 963B 964D 解 对于题目要求,显然一开始的树,要求度数为偶数的节点个数为奇数个,通过奇 ...

  3. window.open全屏显示

    将window.open(url,'','height=600,width=910,top=0,left=0,toolbar=no,menubar=no,scrollbars=yes,resizabl ...

  4. ckeditor粘贴word文档图片的思路

    由于工作需要必须将word文档内容粘贴到编辑器中使用 但发现word中的图片粘贴后变成了file:///xxxx.jpg这种内容,如果上传到服务器后其他人也访问不了,网上找了很多编辑器发现没有一个能直 ...

  5. K8S中Pods

    什么是Pod 一个Pod(就像一群鲸鱼,或者一个豌豆夹)相当于一个共享context的配置组,在同一个context下,应用可能还会有独立的cgroup隔离机制,一个Pod是一个容器环境下的“逻辑主机 ...

  6. seq2seq聊天模型(二)——Scheduled Sampling

    使用典型seq2seq模型,得到的结果欠佳,怎么解决 结果欠佳原因在这里 在训练阶段的decoder,是将目标样本["吃","兰州","拉面" ...

  7. 顺序表应用7:最大子段和之分治递归法(SDUT 3664)

    #include <bits/stdc++.h> using namespace std; const int maxn = 50005; int num = 0; struct node ...

  8. 手写alert弹框(一)

    采用原生的JavaScript, html代码 <meta name="viewport" content="width=device-width, initial ...

  9. Reids入门介绍

    一.入门概述 1.是什么? Redis:REmote DIctionary Server(远程字典服务器) 是完全开源免费的,用C语言编写的,遵守BSD协议,是一个高性能的(key/value)分布式 ...

  10. 在Winform中屏蔽UnityWebPlayer的右键以及自带Logo解决方案整理

    根据项目的需要,对已经完成的Unity三维模型以及游戏要使用Winform进行包装,也就是使用Winform做一层外壳.因此在展示Unity的时候使用到了UnityWebPlayer这个插件,对于此插 ...