date

package
v2.0.0-...-b7a004b Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2025 License: MIT Imports: 6 Imported by: 1

README

Date Library

The Date library provides a convenient wrapper around Go's standard time.Time type with additional functionality for date manipulation, formatting, and calculations. It simplifies working with dates and times in your applications.

Installation

import "github.com/getevo/evo/v2/lib/date"

Features

  • Date Creation: Multiple ways to create dates from different sources (string, time.Time, Unix timestamp)
  • Natural Language Calculations: Calculate relative dates using expressions like "next week" or "2 days after"
  • Time Differences: Calculate durations between dates in various ways
  • Formatting Options: Format dates using both Go's standard format and strftime syntax
  • Timestamp Conversion: Easy conversion to Unix timestamps
  • Midnight Calculation: Get the midnight (start of day) for any date

Usage Examples

Basic Usage
package main

import (
    "fmt"
    "github.com/getevo/evo/v2/lib/date"
)

func main() {
    // Get current date/time
    now := date.Now()
    fmt.Println("Current time:", now.Format("2006-01-02 15:04:05"))
    
    // Get midnight of today
    midnight := date.Now().Midnight()
    fmt.Println("Midnight:", midnight.Format("2006-01-02 15:04:05"))
    
    // Parse a date string
    d, err := date.FromString("2025-08-03 10:26")
    if err != nil {
        fmt.Println("Error:", err)
        return
    }
    fmt.Println("Parsed date:", d.Format("2006-01-02 15:04:05"))
}
Date Calculations
package main

import (
    "fmt"
    "github.com/getevo/evo/v2/lib/date"
)

func main() {
    // Get current date
    now := date.Now()
    
    // Calculate relative dates
    tomorrow, _ := now.Calculate("tomorrow")
    fmt.Println("Tomorrow:", tomorrow.Format("2006-01-02"))
    
    nextWeek, _ := now.Calculate("next week start")
    fmt.Println("Start of next week:", nextWeek.Format("2006-01-02"))
    
    twoMonthsLater, _ := now.Calculate("2 months")
    fmt.Println("Two months from now:", twoMonthsLater.Format("2006-01-02"))
    
    // Calculate time differences
    duration, _ := now.DiffExpr("2 days")
    fmt.Printf("48 hours in seconds: %d\n", duration.Seconds())
}

How It Works

The Date library wraps Go's time.Time type and provides additional methods for common date operations. It uses the dateparse library for flexible string parsing and the strftime library for formatting options.

The core of the library is the Date struct, which contains a time.Time value. All operations are performed on this value, making it easy to chain methods together.

The Calculate method is particularly powerful, allowing for natural language expressions to calculate relative dates. It supports expressions like:

  • "tomorrow", "yesterday", "today"
  • "next week", "last month"
  • "2 days after", "3 months before"
  • "next year start", "this month start"

This makes it easy to perform common date calculations without having to manually manipulate the date components.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Date

type Date struct {
	Base time.Time
}

Date generic date struct

func FromString

func FromString(expr string) (*Date, error)

FromString parse any string to Date

func FromTime

func FromTime(t time.Time) *Date

FromTime parse time to Date

func FromUnix

func FromUnix(sec int64) *Date

FomUnix parse timestamp to Date

func Now

func Now() *Date

Now return current date

func Parse

func Parse(in any) (*Date, error)

func (*Date) Calculate

func (d *Date) Calculate(expr string) (*Date, error)

Calculate calculates relative date to given date

func (*Date) DiffDate

func (d *Date) DiffDate(t Date) time.Duration

DiffDate add date to given date return timestamp

func (*Date) DiffExpr

func (d *Date) DiffExpr(expr string) (time.Duration, error)

DiffExpr add expr to date return timestamp

func (*Date) DiffTime

func (d *Date) DiffTime(t time.Time) time.Duration

DiffTime add given time date return timestamp

func (*Date) DiffUnix

func (d *Date) DiffUnix(t int64) time.Duration

DiffUnix add int64 to given date then return timestamp

func (*Date) Format

func (d *Date) Format(expr string) string

Format formats given date

func (*Date) FormatS

func (d *Date) FormatS(expr string) string

FormatS format given date as strftime syntax

func (*Date) Midnight

func (d *Date) Midnight() *Date

Midnight return midnight of given date

func (*Date) Unix

func (d *Date) Unix() int64

Unix return timestamp of given date

func (*Date) UnixNano

func (d *Date) UnixNano() int64

UnixNano return nano timestamp of given date

Jump to

Keyboard shortcuts

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