fmtdate

package module
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2025 License: BSD-3-Clause Imports: 3 Imported by: 13

README

fmtdate

Note: If you are reading this on Github, please note that the repo has moved to Gitlab (gitlab.com/golang-utils/fmtdate) and this is only a mirror.

100% test coverage (that was easy :-))

fmtdate provides a date formatter and parser using the syntax of Microsoft Excel (TM).

Additionally it offers default conversions for date time and datetime.

Why?

Microsoft Excel (TM) has a well known syntax for date formatting, that more memorable than the syntax chosen in the time package in the go library.

Usage


	package main
	import (
		"gitlab.com/golang-utils/fmtdate"
		"fmt"
	)

	func main() {
		date := fmtdate.Format("DD.MM.YYYY", time.Now())
		fmt.Println(date)

		var err
		date, err = fmtdate.Parse("M/D/YY", "2/3/07")
		fmt.Println(date, err)
	}

For json


    package main

    import (
        "gitlab.com/golang-utils/fmtdate"
        "fmt"
        "encoding/json"
    )

    type Person struct {
        Name string
        BirthDay fmtdate.TimeDate
    }

    func main() {
        bday, err := fmtdate.NewTimeDate("YYYY-MM-DD", "2000-12-04")
        // do error handling
        paul := &Person{"Paul", bday}

        data, err := json.Marshal(paul)
        // do error handling
    }

Formats

M    - month (1)
MM   - month (01)
MMM  - month (Jan)
MMMM - month (January)
D    - day (2)
DD   - day (02)
DDD  - day (Mon)
DDDD - day (Monday)
YY   - year (06)
YYYY - year (2006)
hh   - hours (15)
mm   - minutes (04)
ss   - seconds (05)

AM/PM hours: 'h' followed by optional 'mm' and 'ss' followed by 'pm', e.g.

hpm        - hours (03PM)
h:mmpm     - hours:minutes (03:04PM)
h:mm:sspm  - hours:minutes:seconds (03:04:05PM)

Time zones: a time format followed by 'ZZZZ', 'ZZZ' or 'ZZ', e.g.

hh:mm:ss ZZZZ (16:05:06 +0100)
hh:mm:ss ZZZ  (16:05:06 CET)
hh:mm:ss ZZ   (16:05:06 +01:00)

Documentation

see https://pkg.go.dev/gitlab.com/golang-utils/fmtdate

Documentation

Overview

fmtdate provides a date formatter and parser using the syntax of Microsoft Excel (TM).

Additionally it offers default conversions for date time and datetime.

Why?

Microsoft Excel (TM) has a well known syntax for date formatting, that more memorable than the syntax chosen in the time package in the go library.

Usage

package main
import (
	"gitlab.com/golang-utils/fmtdate"
	"fmt"
)

func main() {
	date := fmtdate.Format("DD.MM.YYYY", time.Now())
	fmt.Println(date)

	var err
	date, err = fmtdate.Parse("M/D/YY", "2/3/07")
	fmt.Println(date, err)
}

Formats

M    - month (1)
MM   - month (01)
MMM  - month (Jan)
MMMM - month (January)
D    - day (2)
DD   - day (02)
DDD  - day (Mon)
DDDD - day (Monday)
YY   - year (06)
YYYY - year (2006)
hh   - hours (15)
mm   - minutes (04)
ss   - seconds (05)

AM/PM hours: 'h' followed by optional 'mm' and 'ss' followed by 'pm', e.g.

hpm        - hours (03PM)
h:mmpm     - hours:minutes (03:04PM)
h:mm:sspm  - hours:minutes:seconds (03:04:05PM)

Time zones: a time format followed by 'ZZZZ', 'ZZZ' or 'ZZ', e.g.

hh:mm:ss ZZZZ (16:05:06 +0100)
hh:mm:ss ZZZ  (16:05:06 CET)
hh:mm:ss ZZ   (16:05:06 +01:00)

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultTimeFormat     = "hh:mm:ss"
	DefaultDateFormat     = "YYYY-MM-DD"
	DefaultDateTimeFormat = "YYYY-MM-DD hh:mm:ss"
)
View Source
var Placeholder = []p{
	{"hh", "15"},
	{"h", "03"},
	{"mm", "04"},
	{"ss", "05"},
	{"MMMM", "January"},
	{"MMM", "Jan"},
	{"MM", "01"},
	{"M", "1"},
	{"pm", "PM"},
	{"ZZZZ", "-0700"},
	{"ZZZ", "MST"},
	{"ZZ", "Z07:00"},
	{"YYYY", "2006"},
	{"YY", "06"},
	{"DDDD", "Monday"},
	{"DDD", "Mon"},
	{"DD", "02"},
	{"D", "2"},
}

Functions

func DaysAgo added in v1.0.5

func DaysAgo(t time.Time, numdaysAgo int) time.Time

func DaysAhead added in v1.0.5

func DaysAhead(t time.Time, numdaysAhead int) time.Time

func FirstDayInMonthsAgo added in v1.0.5

func FirstDayInMonthsAgo(t time.Time, numMonthsAgo int) time.Time

func FirstDayInMonthsAhead added in v1.0.5

func FirstDayInMonthsAhead(t time.Time, numMonthsAhead int) time.Time

func Format

func Format(format string, date time.Time) string

Format formats a date based on Microsoft Excel (TM) conventions

func FormatDate

func FormatDate(date time.Time) string

FormatDate formats the given date to the DefaultDateFormat

func FormatDateTime

func FormatDateTime(date time.Time) string

FormatTime formats the given date to the DefaultDateTimeFormat

func FormatTime

func FormatTime(date time.Time) string

FormatTime formats the given date to the DefaultTimeFormat

func LastDayInMonthsAgo added in v1.0.5

func LastDayInMonthsAgo(t time.Time, numMonthsAgo int) time.Time

func LastDayInMonthsAhead added in v1.0.5

func LastDayInMonthsAhead(t time.Time, numMonthsAhead int) time.Time

func MonthsAgo added in v1.0.5

func MonthsAgo(t time.Time, numMonthsAgo int) time.Time

func MonthsAhead added in v1.0.5

func MonthsAhead(t time.Time, numMonthsAhead int) time.Time

func MustParse added in v1.0.4

func MustParse(format string, value string) time.Time

MustParse is like Parse, but panics on error

func MustParseDate added in v1.0.4

func MustParseDate(value string) time.Time

MustParseDate is like ParseDate, but panics on error

func MustParseDateTime added in v1.0.4

func MustParseDateTime(value string) time.Time

MustParseDateTime is like ParseDateTime, but panics on error

func MustParseTime added in v1.0.4

func MustParseTime(value string) time.Time

MustParseTime is like ParseTime, but panics on error

func Parse

func Parse(format string, value string) (time.Time, error)

Parse parses a value to a date based on Microsoft Excel (TM) formats

func ParseDate

func ParseDate(value string) (time.Time, error)

Parse parses a date in DefaultDateFormat to a date

func ParseDateTime

func ParseDateTime(value string) (time.Time, error)

Parse parses a date in DefaultDateTimeFormat to a date

func ParseTime

func ParseTime(value string) (time.Time, error)

Parse parses a date in DefaultTimeFormat to a date

func SetDay added in v1.0.5

func SetDay(t time.Time, day int) time.Time

func SetMonth added in v1.0.5

func SetMonth(t time.Time, month int) time.Time

func SetYear added in v1.0.5

func SetYear(t time.Time, year int) time.Time

func Translate

func Translate(fmt string) string

Translate translate memorable format to go library's syntax

func YearsAgo added in v1.0.5

func YearsAgo(t time.Time, numYearsAgo int) time.Time

func YearsAhead added in v1.0.5

func YearsAhead(t time.Time, numYearsAhead int) time.Time

Types

type ParseError added in v1.0.3

type ParseError struct {
	Format string
	Value  string
}

func (ParseError) Error added in v1.0.3

func (p ParseError) Error() string

type TimeDate

type TimeDate struct {
	Format string
	*time.Time
}

func NewTimeDate

func NewTimeDate(format, value string) (td TimeDate, err error)

func (TimeDate) IsNil

func (t TimeDate) IsNil() bool

func (TimeDate) MarshalJSON

func (t TimeDate) MarshalJSON() ([]byte, error)

func (*TimeDate) UnmarshalJSON

func (t *TimeDate) UnmarshalJSON(data []byte) error

Jump to

Keyboard shortcuts

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