Golang扩展:DateTime

1. datetime模块编写

package datetime

import "time"

func CurrentDateNotFormat() string {
	timeUnix := time.Now().Unix()
	return time.Unix(timeUnix, 0).Format("20060102")
}

func CurrentDate() string {
	timeUnix := time.Now().Unix()
	return time.Unix(timeUnix, 0).Format("2006-01-02")
}

func CurrentTimeNotFormat() string {
	timeUnix := time.Now().Unix()
	return time.Unix(timeUnix, 0).Format("150405")
}

func CurrentTime() string {
	timeUnix := time.Now().Unix()
	return time.Unix(timeUnix, 0).Format("15:04:05")
}

func CurrentDateTime() string {
	timeUnix := time.Now().Unix()
	return time.Unix(timeUnix, 0).Format("2006-01-02 15:04:05")
}

func CurrentDateTimeNotFormat() string {
	timeUnix := time.Now().Unix()
	return time.Unix(timeUnix, 0).Format("20060102_150405")
}

2. 测试使用

func TestDateTime () {
	fmt.Println(datetime.CurrentDate()) //2020-03-28
	fmt.Println(datetime.CurrentDateNotFormat())//20200328
	fmt.Println(datetime.CurrentTime()) //11:11:26
	fmt.Println(datetime.CurrentTimeNotFormat()) //111126
	fmt.Println(datetime.CurrentDateTime()) //2020-03-28 11:11:26
	fmt.Println(datetime.CurrentDateTimeNotFormat()) //20200328_111126
}

3. 运行结果

Golang扩展:DateTime_第1张图片

你可能感兴趣的:(Golang编程记录)