Fragment Description:



Time formatting.
Go supports time formatting and parsing via pattern-based layouts.
The best way to get used to it, is just to get used to it.
In a word, practical examples are the simplest explanation.


timeFormatOrParse

Go Playground

Last update, on 2015, Fri 9 Oct, 16:15:36

/* ... <== see fragment description ... */

package main

import (
    "fmt"
    "time"
)

func main() {
    p := fmt.Println
    // Here is a basic example of formatting a time
    // according to RFC3339.
    //
    t := time.Now()
    p("-- 1 --\t\t", t.Format("2006-01-02T15:04:05Z07:00"))
    p("--RFC822--\t", t.Format(time.RFC822))
    p("--RFC850--\t", t.Format(time.RFC850))
    p("--ANSIC --\t", t.Format(time.ANSIC))
    p("--RFC1123Z--\t", t.Format(time.RFC1123Z))
    p("========================")
    // "Format" uses an example-based layout approach; it
    // takes a formatted version of the reference time
    // "Mon Jan 2 15:04:05 MST 2006" to determine the
    // general pattern with which to format the given
    // time. The example time must be exactly as shown: the
    // year 2006, 15 for the hour, Monday for the day of the
    // week, etc. Here are a few more examples of time
    // formatting.
    //
    p("--2--    ", t.Format("3:04PM"))
    p("--3--    ", t.Format("Jan 2006, Mon _2, 15h04m05s"))
    p("--4--    ", t.Format("Mon Jan _2 15:04:05 2006"))
    p("--5--    ", t.Format("2006-01-02T15:04:05.999999-07:00"))
    p("========================")
    // For purely numeric representations you can also
    // use standard string formatting with the extracted
    // components of the time value.
    //
    fmt.Printf("--6--    %d-%02d-%02dT%02d:%02d:%02d-00:00\n",
        t.Year(), t.Month(), t.Day(),
        t.Hour(), t.Minute(), t.Second())
    p("========================")
    // Time parsing uses the same example-based approach
    // as "Format"ing. These examples parse times rendered
    // with some of the layouts used above.
    //
    withNanos := "2006-01-02T15:04:05.999999999-07:00"
    t1, err := time.Parse(
        withNanos,
        "2012-11-01T22:08:41.117442+00:00")
    p("--7--    ", t1)
    kitchen := "3:04PM"
    t2, err := time.Parse(kitchen, "8:41PM")
    p("--8--    ", t2)
    p("========================")
    // "Parse" will return an error on malformed input
    // explaining the parsing problem.
    //
    ansic := "Mon Jan _2 15:04:05 2006"
    _, err = time.Parse(ansic, "8:41PM")
    p("--9-- Error: ", err)
    p("========================")
    // There are several predefined formats that you can
    // use for both formatting and parsing.
    //
    p("--10--  ", t.Format(time.Kitchen))
}

/* Expected Output:
-- 1 --         2015-09-15T19:08:26+01:00
--RFC822--  15 Sep 15 19:08 BST
--RFC850--  Tuesday, 15-Sep-15 19:08:26 BST
--ANSIC --  Tue Sep 15 19:08:26 2015
--RFC1123Z--    Tue, 15 Sep 2015 19:08:26 +0100
========================
--2--     7:08PM
--3--     Sep 2015, Tue 15, 19h08m26s
--4--     Tue Sep 15 19:08:26 2015
--5--     2015-09-15T19:08:26.057644+01:00
========================
--6--    2015-09-15T19:08:26-00:00
========================
--7--     2012-11-01 22:08:41.117442 +0000 GMT
--8--     0000-01-01 20:41:00 +0000 UTC
========================
--9--     parsing time "8:41PM" as "Mon Jan _2 15:04:05 2006":
           cannot parse "8:41PM" as "Mon"
========================
--10--   7:08PM
*/



Comments