Time Formats

2,482 views
Skip to first unread message

victorcoder

unread,
Sep 13, 2011, 10:16:43 AM9/13/11
to golan...@googlegroups.com
Sorry if this has been answered before.

I'm writing my first real world app in Go and I need to format a date according to a layout like this 20060102 but, It's possible to specify the format with the widely used %Y%m%d[1] format instead of the standard time layout?

It feels a little weird to me to specify a time format using a time!


peterGo

unread,
Sep 13, 2011, 10:41:43 AM9/13/11
to golang-nuts
victorcoder,

No.

You could write something like this,

package main

import (
"fmt"
"time"
)

func main() {
t := time.UTC()
s := fmt.Sprintf("%04d%02d%02d", t.Year, t.Month+1, t.Day)
fmt.Println(t, "=>", s)
}

Output:
Tue Sep 13 14:40:40 UTC 2011 => 20111013

Peter

Rob 'Commander' Pike

unread,
Sep 13, 2011, 10:52:19 AM9/13/11
to peterGo, golang-nuts
I'm biased but I don't see how that's an improvement over

func main() {
t := time.UTC()

s := t.Format("20060102")
fmt.Println(t, "=>", s)
}

except for knowing the standard date, a fact I believe is more
memorable than all the magic format flags and verbs in strftime, if
not in printf.

-rob

Victor Castell

unread,
Sep 14, 2011, 2:07:16 AM9/14/11
to golang-nuts
Thanks Peter but now, after a second look, I find it easier as Rob states.

No more going to docs for formatting dates, thats's great!
--
www.victorcoder.com | www.season.es | @victorcoder

Tibi

unread,
Sep 15, 2011, 4:04:57 AM9/15/11
to golan...@googlegroups.com, peterGo
I don't understand how that t.Format("20060102") works... why 20060102?

peterGo

unread,
Sep 15, 2011, 4:22:11 AM9/15/11
to golang-nuts
Tibi,

On Sep 15, 4:04 am, Tibi <teabe...@gmail.com> wrote:
> I don't understand how that t.Format("20060102") works... why 20060102?

The Go time package magic time stamp is

Mon Jan 2 15:04:05 MST 2006 (MST is GMT-0700)

or

01/02 03:04:05PM '06 -0700

http://golang.org/pkg/time/#Constants

Peter

Jesse McNelis

unread,
Sep 15, 2011, 4:22:10 AM9/15/11
to golan...@googlegroups.com
On 15/09/11 18:04, Tibi wrote:
> I don't understand how that t.Format("20060102") works... why 20060102?

The time package has documentation that describes why.
http://golang.org/pkg/time/

Time format strings are specified by writing a specific date/time in the
format you'd like. Which is "Mon Jan 2 15:04:05 MST 2006"

"20060102" is just this date in a certain format.
The date 15th September 2011 would be "20110915" in that format.

package main

import "fmt"
import "time"
func main() {
t,err := time.Parse("20060102","20110915")
if err != nil {panic(err)}
fmt.Println(t)
}

will output:
Sun Sep 15 00:00:00 +0000 2011

Note: the day of the week is incorrect because time.Parse() will only
set time values we sent it and I didn't specify a day of the week.

- jessta

Jeff Hodges

unread,
Sep 15, 2011, 7:29:24 AM9/15/11
to jes...@jessta.id.au, golan...@googlegroups.com
On Thu, Sep 15, 2011 at 1:22 AM, Jesse McNelis <jes...@jessta.id.au> wrote:
> Note: the day of the week is incorrect because time.Parse() will only set
> time values we sent it and I didn't specify a day of the week.

Note: this incorrect behavior has been corrected in tip by making
Weekday a method.
http://code.google.com/p/go/source/detail?r=ceeedb519c4a

Kyle Lemons

unread,
Sep 15, 2011, 3:08:15 PM9/15/11
to peterGo, golang-nuts
   01/02 03:04:05PM '06 -0700

I wish the time package documentation printed it out this way...
~K 

Russ Cox

unread,
Sep 15, 2011, 3:23:15 PM9/15/11
to Kyle Lemons, peterGo, golang-nuts
> I wish the time package documentation printed it out this way...

godoc time | grep 01/02

Kyle Lemons

unread,
Sep 15, 2011, 4:35:45 PM9/15/11
to r...@golang.org, peterGo, golang-nuts
> I wish the time package documentation printed it out this way...

godoc time | grep 01/02

Ah.  Apologies.  Apparently my eyes gravitate toward the big grey box and not the line of text under it.
~K

Russ Cox

unread,
Feb 13, 2013, 10:37:53 PM2/13/13
to tko...@google.com, golang-nuts, peterGo
$ godoc time Format
...

    Format returns a textual representation of the time value formatted
    according to layout. The layout defines the format by showing the
    representation of the standard time,

Mon Jan 2 15:04:05 -0700 MST 2006

    which is then used to describe the time to be formatted. Predefined
    layouts ANSIC, UnixDate, RFC3339 and others describe standard
    representations. For more information about the formats and the
    definition of the standard time, see the documentation for ANSIC.

Use t1.Format("2006/01/02"). If you don't use the canonical date in the layout string, Format will not (and cannot) understand what you are telling it to do.

Russ
Reply all
Reply to author
Forward
0 new messages