Search code examples
goposixdatetime-format

Define custom time format


I am trying to write a custom date format string as required by my application. Using the Go time package I get the format using a clumsy function (see below).

Also since this function will be called millions of times every day, I want this to be super efficient too. Is there a POSIX style formatting available in Go?

package main

import (
    "fmt"
    "time"
)

func main() {
    t := time.Now()
    fmt.Printf("Time now is %d%02d%02d%02d%02d%02d",
        t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second())
}

Solution

  • In Go there are built in layouts to pass to the .Format() method of Time (see func (Time) Format), one of these could be what you are looking for:

    func main() {
        fmt.Println(time.Now().Format(time.ANSIC))
        fmt.Println(time.Now().Format(time.UnixDate))
        fmt.Println(time.Now().Format(time.RFC3339))
    }
    

    This is going to be faster than fmt.Printf("...", t.Year(), t.Month(), ...), but we're talking about microseconds "faster" here, so there really is no big difference.

    Output:

    Sun Jun 10 13:18:09 2018
    Sun Jun 10 13:18:09 UTC 2018
    2018-06-10T13:18:09Z
    

    There are a lot more predefined layouts, so you can check all of them out and see if one of them fits your needs. Here they are directly from the source code:

    const (
        ANSIC       = "Mon Jan _2 15:04:05 2006"
        UnixDate    = "Mon Jan _2 15:04:05 MST 2006"
        RubyDate    = "Mon Jan 02 15:04:05 -0700 2006"
        RFC822      = "02 Jan 06 15:04 MST"
        RFC822Z     = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone
        RFC850      = "Monday, 02-Jan-06 15:04:05 MST"
        RFC1123     = "Mon, 02 Jan 2006 15:04:05 MST"
        RFC1123Z    = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone
        RFC3339     = "2006-01-02T15:04:05Z07:00"
        RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
        Kitchen     = "3:04PM"
        // Handy time stamps.
        Stamp      = "Jan _2 15:04:05"
        StampMilli = "Jan _2 15:04:05.000"
        StampMicro = "Jan _2 15:04:05.000000"
        StampNano  = "Jan _2 15:04:05.000000000"
    )
    

    Other than that, it's just a matter of creating your own layout, there really is no difference.


    To create a custom layout string, quoting from the documentation:

    The layout string used by the Parse function and Format method shows by example how the reference time should be represented. We stress that one must show how the reference time is formatted, not a time of the user's choosing. Thus each layout string is a representation of the time stamp: Jan 2 15:04:05 2006 MST.

    So, for example:

    fmt.Println(time.Now().Format("Mon January 2, 2006 - 15:04:05.000"))
    

    gives:

    Sun June 10, 2018 - 17:49:32.557
    

    If you want something different you'll just have to format the date Jan 2 15:04:05 2006 MST how you would want it to be displayed. You can also take a look at the relative page on Go by Example.