go-scan/util/date/date.go
2022-06-24 15:31:48 +02:00

64 lines
1.2 KiB
Go

package date
import (
"fmt"
"time"
)
// exported functions
func New() Date {
now := time.Now()
day := fmt.Sprint(now.Day())
if len(day) < 2 {
day = "0" + day
}
month := fmt.Sprint(int(now.Month()))
if len(month) < 2 {
month = "0" + month
}
year := fmt.Sprint(now.Year())
hour := fmt.Sprint(now.Hour())
if len(hour) < 2 {
hour = "0" + hour
}
minute := fmt.Sprint(now.Minute())
if len(minute) < 2 {
minute = "0" + minute
}
second := fmt.Sprint(now.Second())
if len(second) < 2 {
second = "0" + second
}
return Date{
Day: day,
Month: month,
Year: year,
Hour: hour,
Minute: minute,
Second: second,
GetFormattedDate: func() string {
return day + "." + month + "." + year + " " + hour + ":" + minute + ":" + second
},
}
}
func Milliseconds() int {
return int(time.Now().UnixMilli())
}
func GetTimeDifference(timestamp int) int {
return Milliseconds() - timestamp
}
// structs
type getFormattedDate func() string
type Date struct {
Day string
Month string
Year string
Hour string
Minute string
Second string
GetFormattedDate getFormattedDate
}