datetime

package module
v1.3.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 2, 2025 License: MIT Imports: 6 Imported by: 17

README

DateTime - Simple DateTime Engine for Go

PkgGoDev Build Status Go Report Card Coverage Status GitHub issues Release

Installation

To install the package, run:

go get github.com/go-zoox/datetime

Getting Started

fmt.Println(datetime.Now().Format("YYYY-MM-DD HH:mm:ss"))
// 2022-04-12 13:05:50

Inspired by

  • zcorky/moment - A minimalist JavaScript library that parses, validates, manipulates, and displays dates and times.
  • dayjs
  • moment.js

License

GoZoox is released under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AgoPattern = map[string]string{
	"now":    "just now",
	"second": "%d second ago",
	"minute": "%d minute ago",
	"hour":   "%d hour ago",
	"day":    "%d day ago",
	"week":   "%d week ago",
	"month":  "%d month ago",
	"year":   "%d year ago",
}

AgoPattern is the pattern for Ago.

View Source
var DefaultFormatPattern = "YYYY-MM-DD HH:mm:ss"

DefaultFormatPattern is the default format pattern.

View Source
var Language = "en-US"

Language is the language.

View Source
var Locales = I18nLocales{}

Locales is the locales.

View Source
var LocalesWeekDays = []string{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}

LocalesWeekDays is the week days.

View Source
var LocalesWeekDaysMin = []string{"Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"}

LocalesWeekDaysMin is the week days in minium.

View Source
var LocalesWeekDaysShort = []string{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}

LocalesWeekDaysShort is the week days in short.

View Source
var TimeZoneForce = ""

TimeZoneForce will force to override datetime instance time zone

	default is empty string, means no force time zone, use datetime self timezone, it is local timezone
 for example, set as Asia/Shanghai,
		Format will use Asia/Shanghai override internal time zone
	  unless it has call SetTimeZone before
View Source
var Version = "1.3.2"

Version is the version of this package.

Functions

func SetLanguage added in v1.2.0

func SetLanguage(language string)

SetLanguage sets the language.

func SetLocales added in v1.2.0

func SetLocales(locales I18nLocales)

SetLocales is the week days.

func SetTimeZone added in v1.1.0

func SetTimeZone(timezone string)

SetTimeZone sets the global time zone of the datetime.

Types

type AgoOptions added in v1.2.0

type AgoOptions struct {
	Diff *DateTime
}

AgoOptions is the options for Ago.

type DateTime

type DateTime struct {
	// contains filtered or unexported fields
}

DateTime is a struct that represents a datetime.

func FromDate

func FromDate(year, month, day, hour, minute, second int, timezone ...string) (*DateTime, error)

FromDate returns a DateTime from a Date. Example:

FromDate(2022, 4, 12, 10, 39, 12)
FromDate(2022, 4, 12, 10, 39, 12, "Asia/Shanghai")

func FromPattern

func FromPattern(pattern string, datetime string, timezone ...string) (*DateTime, error)

FromPattern returns a DateTime from a pattern(layout) and a datetime string. Example:

	 pattern := "YYYY/MM/DD HH:mm:ss"
	 datetime := "2022/04/12 10:39:12"
	 dt, err := FromPattern(pattern, datetime)
	 dt, err := FromPattern(pattern, datetime, "Asia/Shanghai")

Available pattern(layout): (file: /usr/local/go/src/time/format.go)
   time.Layout      = "01/02 03:04:05PM '06 -0700" // The reference time, in numerical order.
   time.ANSIC       = "Mon Jan _2 15:04:05 2006"
   time.UnixDate    = "Mon Jan _2 15:04:05 MST 2006"
   time.RubyDate    = "Mon Jan 02 15:04:05 -0700 2006"
   time.RFC822      = "02 Jan 06 15:04 MST"
   time.RFC822Z     = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone
   time.RFC850      = "Monday, 02-Jan-06 15:04:05 MST"
   time.RFC1123     = "Mon, 02 Jan 2006 15:04:05 MST"
   time.RFC1123Z    = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone
   time.RFC3339     = "2006-01-02T15:04:05Z07:00"
   time.RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
   time.Kitchen     = "3:04PM"
   // Handy time stamps.
   time.Stamp      = "Jan _2 15:04:05"
   time.StampMilli = "Jan _2 15:04:05.000"
   time.StampMicro = "Jan _2 15:04:05.000000"
   time.StampNano  = "Jan _2 15:04:05.000000000"

func FromTime

func FromTime(t time.Time) *DateTime

FromTime returns a DateTime from time.Time. Example:

t := time.Now()
dt := FromTime(t)

func New

func New(args ...interface{}) (*DateTime, error)

New returns a DateTime Support:

  1. time.Time example: New(time.Now())

  2. YYYY/MM/DD HH:mm:ss, datetime string example: New("YYYY/MM/DD HH:mm:ss", "2022/04/12 10:39:12") New("YYYY/MM/DD HH:mm:ss", "2022/04/12 10:39:12", "Asia/Shanghai")

  3. year, month, day, hour, minute, second example: New(2022, 4, 12, 10, 39, 12) New(2022, 4, 12, 10, 39, 12, "Asia/Shanghai")

func Now

func Now() *DateTime

Now returns the time.Time of DateTime.

func (*DateTime) Add

func (dt *DateTime) Add(d time.Duration) *DateTime

Add adds the duration to the datetime.

func (*DateTime) AddDate

func (dt *DateTime) AddDate(years int, months int, days int) *DateTime

AddDate adds the specified years, months, and days to the datetime.

func (*DateTime) After

func (dt *DateTime) After(t *DateTime) bool

After returns true if the datetime is after the other datetime.

func (*DateTime) Ago added in v1.2.0

func (dt *DateTime) Ago(opts ...*AgoOptions) string

Ago return time ago from now.

func (*DateTime) Before

func (dt *DateTime) Before(t *DateTime) bool

Before returns true if the datetime is before the other datetime.

func (*DateTime) Date added in v1.0.3

func (dt *DateTime) Date() (year, month, day int)

Date returns the date.

func (*DateTime) Day

func (dt *DateTime) Day() int

Day returns the day of the month.

func (*DateTime) EndOfDay

func (dt *DateTime) EndOfDay() *DateTime

EndOfDay returns the end of the day.

func (*DateTime) EndOfHour

func (dt *DateTime) EndOfHour() *DateTime

EndOfHour returns the end of the hour.

func (*DateTime) EndOfMinute added in v1.0.3

func (dt *DateTime) EndOfMinute() *DateTime

EndOfMinute returns the end of the hour.

func (*DateTime) EndOfMonth

func (dt *DateTime) EndOfMonth() *DateTime

EndOfMonth returns the end of the month.

func (*DateTime) EndOfQuarter added in v1.0.3

func (dt *DateTime) EndOfQuarter() *DateTime

EndOfQuarter returns the end of the quarter.

func (*DateTime) EndOfWeek

func (dt *DateTime) EndOfWeek() *DateTime

EndOfWeek returns the end of the week.

func (*DateTime) EndOfYear

func (dt *DateTime) EndOfYear() *DateTime

EndOfYear returns the end of the year.

func (*DateTime) Equal

func (dt *DateTime) Equal(t *DateTime) bool

Equal reports whether t and dt represent the same time instant.

func (*DateTime) Format

func (dt *DateTime) Format(pattern ...string) string

Format return the format of time with pattern/layout

func (*DateTime) GetTimeZone added in v1.1.0

func (dt *DateTime) GetTimeZone() string

GetTimeZone gets the time zone of the datetime.

func (*DateTime) Hour

func (dt *DateTime) Hour() int

Hour returns the hour.

func (*DateTime) Location

func (dt *DateTime) Location() *time.Location

Location returns the location.

func (*DateTime) MarshalJSON added in v1.3.1

func (dt *DateTime) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (*DateTime) Millisecond

func (dt *DateTime) Millisecond() int

Millisecond returns the milliseconds.

func (*DateTime) Minute

func (dt *DateTime) Minute() int

Minute returns the minutes.

func (*DateTime) Month

func (dt *DateTime) Month() int

Month returns the month.

func (*DateTime) Second

func (dt *DateTime) Second() int

Second returns the seconds.

func (*DateTime) SetTimeZone

func (dt *DateTime) SetTimeZone(timezone string) error

SetTimeZone sets the time zone of the datetime.

func (*DateTime) StartOfDay

func (dt *DateTime) StartOfDay() *DateTime

StartOfDay returns the start of the day.

func (*DateTime) StartOfHour

func (dt *DateTime) StartOfHour() *DateTime

StartOfHour returns the start of the hour.

func (*DateTime) StartOfMinute added in v1.0.3

func (dt *DateTime) StartOfMinute() *DateTime

StartOfMinute returns the start of the hour.

func (*DateTime) StartOfMonth

func (dt *DateTime) StartOfMonth() *DateTime

StartOfMonth returns the start of the month.

func (*DateTime) StartOfQuarter added in v1.0.3

func (dt *DateTime) StartOfQuarter() *DateTime

StartOfQuarter returns the end of the hour.

func (*DateTime) StartOfWeek

func (dt *DateTime) StartOfWeek() *DateTime

StartOfWeek returns the start of the week.

func (*DateTime) StartOfYear

func (dt *DateTime) StartOfYear() *DateTime

StartOfYear returns the start of the year.

func (*DateTime) String added in v1.0.3

func (dt *DateTime) String() string

String returns the string representation of the datetime.

func (*DateTime) Sub

func (dt *DateTime) Sub(t *DateTime) time.Duration

Sub subtracts the duration from the datetime.

func (*DateTime) Time

func (dt *DateTime) Time() time.Time

Time returns the time.Time.

func (*DateTime) Timestamp

func (dt *DateTime) Timestamp() int64

Timestamp return the miliseconds since the Unix epoch.

func (*DateTime) Unix added in v1.3.0

func (dt *DateTime) Unix() int64

Unix returns the number of seconds since the Unix epoch.

func (*DateTime) UnixMilli added in v1.3.0

func (dt *DateTime) UnixMilli() int64

UnixMilli returns the number of milliseconds since the Unix epoch.

func (*DateTime) UnixNano added in v1.3.0

func (dt *DateTime) UnixNano() int64

UnixNano returns the number of nanoseconds since the Unix epoch.

func (*DateTime) UnmarshalJSON added in v1.3.1

func (dt *DateTime) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (*DateTime) WeekDay

func (dt *DateTime) WeekDay() int

WeekDay returns the day of week

0 - Sunday
1 - Monday
2 - Tuesday
3 - Wednesday
4 - Thursday
5 - Friday
6 - Sataurday

func (*DateTime) Year

func (dt *DateTime) Year() int

Year returns the year.

type I18nLocales added in v1.2.0

type I18nLocales map[string]I18nTranslations

I18nLocales is the locales.

type I18nTranslations added in v1.2.0

type I18nTranslations map[string]string

I18nTranslations is the translations.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL