fortrieb
fortrieb

Reputation: 23

Go time.Format wrong month

I'd like to rename some files based on their modification date.

When I use the time.Format method to get the correct string, basically in this format YYYY-MM-DD_HH-MM-SS, the day has a trailing 0.

Am I doing something wrong here?

package main

import (
    "time"
    "fmt"
)

func main() {

    loc, _ := time.LoadLocation("Europe/Berlin")

    const layout = "2006-01-20_15-04-05"

    t := time.Date(2013, 07, 23, 21, 32, 39, 0, loc)
    fmt.Println(t)
    fmt.Println(t.Format(layout))
}

Click to play

Output:

2013-07-23 21:32:39 +0200 CEST
2013-07-230_21-32-39

Upvotes: 2

Views: 589

Answers (1)

JimB
JimB

Reputation: 109330

Your layout isn't using the reference date: change it to "2006-01-02_15-04-05"

When you use "2006-01-20_15-04-05", the formatter see the 2, and uses that for the day, then keeps the extra 0 since it doesn't match any part of the reference date.

Upvotes: 6

Related Questions