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
Closed

time: want more control over time.Format #9547

ddietterich opened this issue Jan 9, 2015 · 6 comments

Comments

@ddietterich
Copy link

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
Copy link
Contributor

bradfitz 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 as completed Jan 9, 2015
@cespare
Copy link
Contributor

cespare commented Jan 9, 2015

@ddietterich
Copy link
Author

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
Copy link
Author

@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
Copy link
Contributor

cespare 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
Copy link
Author

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.
Projects
None yet
Development

No branches or pull requests

4 participants