Standard library / time

time.Time.MarshalJSON()

Return a JSON string containing an RFC3339 timestamp with fractional seconds as needed; out-of-range years or offsets may cause errors. Expected stdout: '"2024-02-29T12:34:56.123Z"\n'.

Formal Syntax & Structure

go syntaxgo
func (t Time) MarshalJSON() ([]byte, error)

Return Value

([]byte, error)

Return a JSON string containing an RFC3339 timestamp with fractional seconds as needed; out-of-range years or offsets may cause errors.

Working Example

Example Snippetgo
package main
import (  "fmt"  "time")
func fixed() time.Time { return time.Date(2024, 2, 29, 12, 34, 56, 123000000, time.UTC) }func must(err error) {  if err != nil {    panic(err)  }}func main() { data, err := fixed().MarshalJSON(); must(err); fmt.Println(string(data)) }
Go Reference