icarbajo
icarbajo

Reputation: 351

Add zero padding to numbers with fmt.Printf

I have a question about the function fmt.Printf; Why when I use this functions, the program omits the first zero?

package main

import (
   "fmt"
   "time"
)

func main() {
   now := time.Now()
   year, month, day := now.Date()
   hour, min, sec := now.Clock()
   fmt.Printf("%d-%s-%d_%d:%d:%d\n", year, month, day, hour, min, sec)
}

That returns: 2017-April-26_10:3:2 at 10h03:02

Can anyone help me?

Upvotes: 0

Views: 152

Answers (1)

putu
putu

Reputation: 6444

Simply replace %d with %02d i.e.

fmt.Printf("%d-%s-%02d_%02d:%02d:%02d\n", year, month, day, hour, min, sec)

Upvotes: 3

Related Questions