小工具      在线工具  汉语词典  dos游戏  css  js  c++  java

go语言对时间的操作及解析

Go,golang 额外说明

收录于:17天前

  • time.TimeYYYY-MM-DD hh:mm:ss
import (
	"database/sql"
	"fmt"
	"time"
)

// FormatMysqlTime 格式化MysqlTime
func FormatMysqlTime(time time.Time) string {
    
	if time.IsZero() {
    
		return ""
	}
	msg := time.Format("2006-01-02 15:04:05")
	return msg
}
  • time.Time 转 sql.NullTime

func GenMysqlTime(t time.Time) sql.NullTime {
    
	return sql.NullTime{
    
		Time:  t,
		Valid: true,
	}
}

// 时间转换 string to time.Time
func GetTime(t string) time.Time {
    
	parseTime, _ := time.Parse("2006-01-02 15:04:05", t)
	return parseTime
}

  • 获取当天00:00:00日期
// 获取当天00:00:00日期
func GetNowZeroTime() string {
    
	zt, _ := time.ParseInLocation("2006-01-02", time.Now().Format("2006-01-02"), time.Local)
	return zt.Format("2006-01-02 15:04:05")
}

  • 获取前七天日期
// 获取前七天日期
func GetListSevenDays() []string {
    
	var times []string
	// 时区
	timeZone := time.FixedZone("CST", 8*3600) // 东八区
	// 前n天
	nowTime := time.Now().In(timeZone)

	for i := -7; i < 0; i++ {
    
		beforeTime := nowTime.AddDate(0, 0, i).Format("2006-01-02")
		times = append(times, beforeTime)
	}

	return times
}

  • 获取前一个月(自然月)日期
// 获取前一个月(自然月)日期
func GetListMonth() []string {
    
	var times []string
	// 时区
	timeZone := time.FixedZone("CST", 8*3600) // 东八区

	// 前n天
	nowTime := time.Now().In(timeZone)
	currentTime, _ := time.Parse("2006/01/02", time.Now().Format("2006/01/02"))
	startTime, _ := time.Parse("2006/01/02", nowTime.AddDate(0, -1, 0).Format("2006/01/02"))
	//天数
	dis := currentTime.Sub(startTime)
	loveDay := dis.Hours() / 24
	//添加日期
	for i := -int(loveDay); i < 0; i++ {
    
		beforeTime := nowTime.AddDate(0, 0, i).Format("2006-01-02")
		times = append(times, beforeTime)
	}
	return times
}
  • 获取前六月日期
// 获取前六月日期
func GetListSixMonth() []string {
    
	var times []string
	// 时区
	timeZone := time.FixedZone("CST", 8*3600) // 东八区
	// 前n天
	nowTime := time.Now().In(timeZone)

	for i := -6; i < 0; i++ {
    
		beforeTime := nowTime.AddDate(0, i, 0).Format("2006-01")
		times = append(times, beforeTime)
	}

	return times
}
  • 获取前一年日期
// 获取前一年日期
func GetListYear() []string {
    
	var times []string
	// 时区
	timeZone := time.FixedZone("CST", 8*3600) // 东八区
	// 前n天
	nowTime := time.Now().In(timeZone)

	for i := -12; i < 0; i++ {
    
		beforeTime := nowTime.AddDate(0, i, 0).Format("2006-01")
		times = append(times, beforeTime)
	}

	return times
}

  • 复杂时间字符串的转换
// 复杂时间字符串的转换
// `2023-09-20T00:00:00+08:00` 转 `2006-01-02 15:04:05` 格式
func ComplexStringToTime(str string) string {
    
	layout := "2006-01-02T15:04:05-07:00"
	// 解析输入的时间字符串
	t, err := time.Parse(layout, str)
	if err != nil {
    
		fmt.Println("解析错误:", err)
		return ""
	}
	formatted := t.Format("2006-01-02 15:04:05")
	return formatted
}

. . .

相关推荐

额外说明

/bin/bash^M: 坏的解释器: 没有那个文件或目录

运行shell脚本 时 错误原因 这个文件在Windows 下编辑过,在Windows下每一行结尾是\n\r,而Linux下则是\n,所以才会有 多出来的\r。 修改错误 使用指令sed -i 's/\r$//' xxxxxxx.sh,上面的指令会把 x

额外说明

influxdb的基本使用

influxDB名词 database:数据库; measurement:数据库中的表; points:表里面的一行数据。 influxDB中独有的一些概念 Point由时间戳(time)、数据(field)和标签(tags)组成。 time:每条数据记

额外说明

阿里云oss上传视频测试,出现了413错误

阿里云oss上传视频测试,出现了413错误 (1)nginx抛出问题,请求体过大 (2)修改nginx配置,重新加载生效 client_max_body_size 1024m; 在cmd下运行命令:nginx.exe -s reload

额外说明

idea导入新项目报异常:Error:java: Compilation failed: internal java compiler error

修改settings中项目对象Java编译配置恢复正常。 标记的两个地方配置修改和项目一样后,正常。

额外说明

Servlet&HTTP&Request笔记

文章目录 Servlet HTTP Request 案例:用户登录 Servlet Servlet的体系结构 Servlet – 接口 | GenericServlet – 抽象类 | HttpServlet – 抽象类 GenericServlet:将

额外说明

Python实现机器学习(下)— 数据预处理、模型训练和模型评估

前言:Hello大家好,我是小哥谈。本门课程将介绍人工智能相关概念,重点讲解机器学习原理机器基本算法(监督学习及非监督学习)。使用python,结合sklearn、Pycharm进行编程,介绍iris(鸢尾花)数据集,建立AI模型并评估其表现。本节课主要

额外说明

jdk 新特性 CompletableFuture 并行框架

一、执行异步任务supplyAsync\runAsync CompletableFuture 提供了四个静态方法来创建一个异步操作。 //不支持返回值。 public static CompletableFuture<Void> runAsync(Run

额外说明

【Java面试小短文】Cookie和Session的区别

欢迎关注Java面试系列,不定期更新面试小短文。欢迎一键三连! Cookie和Session的区别   Cookie是客户端浏览器用来保存服务端数据的一种机制,当我们通过浏览器去进行网页访问的时候,服务器可以把某一些状态数据以key-value的形式写入

额外说明

安卓案例:联选系部与专业

文章目录 一、下拉列表概述 二、案例演示 - 联选系部与专业 (一)运行效果 (二)涉及知识点 (三)实现步骤 1、创建安装应用【ChooseDepartmentMajor】 2、将图片素材拷贝到drawable目录 3、主布局资源文件activity_

额外说明

解决Windows系统目录console.dll文件丢失找不到问题

其实很多用户玩单机游戏或者安装软件的时候就出现过这种问题,如果是新手第一时间会认为是软件或游戏出错了,其实并不是这样,其主要原因就是你电脑系统的该dll文件丢失了或没有安装一些系统软件平台所需要的动态链接库,这时你可以下载这个console.dll文件(

ads via 小工具