Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

time: want more control over time.Format #9547

Closed
ddietterich opened this issue Jan 9, 2015 · 6 comments

Comments

Projects
None yet
4 participants
@ddietterich
Copy link

commented Jan 9, 2015

Using go version go1.3.3 linux/amd64

func main() {
        t := time.Now()
        fmt.Printf("time: %v; format: %s\n", t, t.Format("20080101 10:00:00.000"))
}

Prints:

time: 2015-01-09 17:06:02.033307544 +0000 UTC; format: 90080101 10:00:00.033

I want YYYYMMDD, but time.Format is only layout by example, so it cannot know what digits mean year, month, day. I have to do it by hand.

@bradfitz

This comment has been minimized.

Copy link
Member

commented Jan 9, 2015

Use the reference time. This is explained in the docs: http://golang.org/pkg/time/#pkg-constants

@bradfitz bradfitz closed this Jan 9, 2015

@cespare

This comment has been minimized.

Copy link
Contributor

commented Jan 9, 2015

@ddietterich

This comment has been minimized.

Copy link
Author

commented Jan 9, 2015

I read that documentation. I find nothing there that lets me distinguish years, months and days in a concatenated string. It just will not do what I want it to do.

@ddietterich

This comment has been minimized.

Copy link
Author

commented Jan 9, 2015

@cespare Maybe there is a fix between 1.3.3 and 1.4? You can see that I did not get the correct year in my example.

@cespare

This comment has been minimized.

Copy link
Contributor

commented Jan 9, 2015

@ddietterich as @bradfitz said, you need to use the reference time, not some other time of your choosing. (Examine carefully the difference between your code and mine.)

@ddietterich

This comment has been minimized.

Copy link
Author

commented Jan 9, 2015

OK, I get it.
I find it entirely obscure and won't use it, but I cannot complain that it won't do what I want it to do.
Thanks!

@golang golang locked and limited conversation to collaborators Jun 25, 2016

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.