golang 时间工具类

用不习惯也嫌麻烦每次都去操作时间,然后就自己写了个时间工具类

go 复制代码
package timeutil

import (
	"time"
)

func New() *TimeUtil {
	return &TimeUtil{}
}

// TimeUtil 是时间操作工具类
type TimeUtil struct{}

// GetFormattedDate 获取格式化的日期字符串
func (tu *TimeUtil) GetFormattedDate(t time.Time) string {
	return t.Format("2006-01-02")
}

// GetFormattedTime 获取格式化的时间字符串
func (tu *TimeUtil) GetFormattedTime(t time.Time) string {
	return t.Format("15:04:05")
}

// GetFormattedDateTime 获取格式化的日期时间字符串
func (tu *TimeUtil) GetFormattedDateTime(t time.Time) string {
	return t.Format("2006-01-02 15:04:05")
}

// GetWeekday 获取星期几
func (tu *TimeUtil) GetWeekday(t time.Time) string {
	return t.Weekday().String()
}

// IsLeapYear 判断是否为闰年
func (tu *TimeUtil) IsLeapYear(year int) bool {
	return year%4 == 0 && (year%100 != 0 || year%400 == 0)
}

// ParseDateFromString 将日期字符串解析为时间对象
func (tu *TimeUtil) ParseDateFromString(dateString string) (time.Time, error) {
	layout := "2006-01-02"
	t, err := time.Parse(layout, dateString)
	if err != nil {
		return time.Time{}, err
	}
	return t, nil
}

// ParseTimeFromString 将时间字符串解析为时间对象
func (tu *TimeUtil) ParseTimeFromString(timeString string) (time.Time, error) {
	layout := "15:04:05"
	t, err := time.Parse(layout, timeString)
	if err != nil {
		return time.Time{}, err
	}
	return t, nil
}

// ParseDateTimeFromString 将日期时间字符串解析为时间对象
func (tu *TimeUtil) ParseDateTimeFromString(dateTimeString string) (time.Time, error) {
	layout := "2006-01-02 15:04:05"
	t, err := time.Parse(layout, dateTimeString)
	if err != nil {
		return time.Time{}, err
	}
	return t, nil
}

// FormatTimeString 将时间字符串转换为指定格式的日期时间字符串
/**
inputTime 表示输入的时间字符串 字符格式为:2006-01-02 15:04:05
format 表示要转换的日期时间格式 1、YYYY-MM-DD hh:mm:ss  2、YYYY-MM-DD  3、hh:mm:ss
*/
func (tu *TimeUtil) FormatTimeString(inputTime, format string) (string, error) {
	format = rule(format)
	layout := "2006-01-02 15:04:05"
	t, err := time.Parse(layout, inputTime)
	if err != nil {
		return "", err
	}

	formattedTime := t.Format(format)
	return formattedTime, nil
}

// FormatTimeStringZ 将时间字符串转换为指定格式的日期时间字符串
/**
inputTime 表示输入的时间字符串 字符格式为:2023-07-29T15:30:00Z
format 表示要转换的日期时间格式 1、YYYY-MM-DD hh:mm:ss  2、YYYY-MM-DD  3、hh:mm:ss
*/
func (tu *TimeUtil) FormatTimeStringZ(inputTime, format string) (string, error) {
	format = rule(format)
	t, err := time.Parse(time.RFC3339, inputTime)
	if err != nil {
		return "", err
	}
	formattedTime := t.Format(format)
	return formattedTime, nil
}

// rule 时间规则
func rule(format string) (str string) {
	if format == "1" {
		format = "2006-01-02 15:04:05"
	} else if format == "2" {
		format = "2006-01-02"
	} else if format == "3" {
		format = "15:04:05"
	}
	return format
}
相关推荐
SelectDB2 小时前
开源日志平台怎么选?ELK、Loki、ClickHouse、Apache Doris 四大方案对比与选型指南
数据库
SelectDB3 小时前
ELK 磁盘空间不足怎么办?Elasticsearch 存储膨胀原因、排查步骤与 3 种降本方案
数据库
满小满3 小时前
为什么我先 ANALYZE,而不是先加索引
数据库·dba
geovindu3 小时前
rust: Functional Options Pattern
开发语言·后端·设计模式·rust·函数式选项模式·惯用法型模式
SelectDB3 小时前
日志分析用什么数据库好?Elasticsearch、ClickHouse、Doris 选型对比与 3 个企业案例
数据库
skywalk81634 小时前
把「假成功」赶出工具层:lightharness 两天升级复盘(4×5 模式矩阵 + 工具层 5 个缺陷)
开发语言·前端·人工智能·lightharness
敲上瘾4 小时前
Qt控件通用属性:geometry、qrc资源到QSS样式表等
开发语言·c++·qt·ui
inferno5 小时前
十三、存储过程(PROCEDURE)
数据库·sql·mysql
听情歌落俗5 小时前
嵌入式学习1-C语言基础语法与STM32实践
c语言·开发语言
数据库急诊室_阿哲5 小时前
一周慢查询日志审计:1200 条里有多少真问题
运维·数据库·dba