JSON date formats for Go.
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
example
LICENSE
README.md
all_test.go
yearmonthday.go
yearmonthdayhoursminutesseconds.go

README.md

date

JSON date formats for Go.

Marshal and unmarshal date times in yyyy-MM-dd and yyyy-MM-ddThh:mm:ss formats.

{
  "ymd":"2008-12-25",
  "ymdhms":"1742-12-25T13:32:20"
}

Example usage

package main

import (
	"encoding/json"
	"fmt"

	"github.com/a-h/date"
)

type example struct {
	YMD    date.YYYYMMDD       `json:"ymd"`
	YMDHMS date.YYYYMMDDHHMMSS `json:"ymdhms"`
}

func main() {
	j := `{"ymd":"2008-12-25","ymdhms":"1742-12-25T13:32:20"}`
	var e example
	err := json.Unmarshal([]byte(j), &e)
	if err != nil {
		fmt.Println("Error unmarshalling data: ", err)
		return
	}
	fmt.Println(e.YMD)
	fmt.Println(e.YMDHMS)
	output, err := json.Marshal(e)
	if err != nil {
		fmt.Println("Error marshalling data: ", err)
		return
	}
	fmt.Println(string(output)) // {"ymd":"2008-12-25","ymdhms":"1742-12-25T13:32:20"}
}