[Go] Parse a date-time string into time.Time

Created on November 30, 2026

You can use time.Parse to convert a date-time string into a time.Time value. The first argument is the layout that describes the input, and the second is the string to parse.

package main

import (
    "fmt"
    "time"
)

func main() {
    layout := "2006-01-02 15:04:05 -07:00 MST"
    t, err := time.Parse(layout, "2019-05-15 11:22:33 +09:00 JST")
    if err != nil {
        panic(err)
    }
    fmt.Println(t.String())
}
2019-05-15 11:22:33 +0900 JST

Go layouts use the reference time shown in layout to identify each field. The numeric offset in the input specifies the time unambiguously even if the local time zone does not recognize the abbreviation JST.

Go