datetime

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2022 License: MIT Imports: 4 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 DefaultFormatPattern = "YYYY-MM-DD HH:mm:ss"

DefaultFormatPattern is the default format pattern.

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.

Functions

This section is empty.

Types

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 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")

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) Before

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

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

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) EndOfMonth

func (dt *DateTime) EndOfMonth() *DateTime

EndOfMonth returns the end of the month.

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) 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) 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) StartOfMonth

func (dt *DateTime) StartOfMonth() *DateTime

StartOfMonth returns the start of the month.

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) 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) 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.

Jump to

Keyboard shortcuts

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