Skip to content

perron2/go-date

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Date-Only Custom Type

A Go package to work with civil date-only values (year, month, day without a time zone).

Install

go get github.com/perron2/go-date

Requires Go 1.25+.

Quick Start

package main

import (
	"encoding/json"
	"fmt"
	"github.com/perron2/go-date"
	"time"
)

func main() {
    today := date.Today()
    earlier := date.New(2024, time.August, 1)
    days := today.DaysSince(earlier)
    fmt.Printf("%d days since %s\n", days, today)
    
    twoYearsLater := today.AddYears(2)
    if today.Before(twoYearsLater) {
        fmt.Println("today is before twoYearsLater")
    }
    if twoYearsLater.After(today) {
       fmt.Println("twoYearsLater is after today")
    }
    
    jsonStruct := struct {
        Start date.Date `json:"start"`
        End   date.Date `json:"end"`
    }{date.MustParse("2025-08-01"), date.MustParse("2025-09-30")}
    
    jsonData, _ := json.Marshal(jsonStruct)
    fmt.Println(string(jsonData))
}

About

A date-only custom type for Go

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages