3

This is a part of a bigger code. I am little confused that if I use any other digit in the below format it results in wrong values.

so instead of

fmt.Println(time.Now().Format("2006-01-02 15:04:05-07:00"))

if I try to use

 fmt.Println(time.Now().Format("2006-01-02 12:04:05-04:00"))

Result is wrong. Even when it is same format, just digit change

package main

import (
    "fmt"
    "time"
)


func main() {

    fmt.Println(time.Now().Format("2006-01-02 15:04:05-07:00"))

}

So my question is why is it so. Digits inside format have no meaning. They are just for representation of the format.

codeforester
  • 39,467
  • 16
  • 112
  • 140
Tahseen
  • 1,027
  • 1
  • 12
  • 32
  • What do you mean the digits have no meaning? They _are_ the representation of the format. – JimB Apr 02 '17 at 02:34
  • the digits inside Format, do they really make a difference whether 2006 or 2005 ? – Tahseen Apr 02 '17 at 02:40
  • Yes. How else would you differentiate the numbers if the values didn't matter? – JimB Apr 02 '17 at 02:44
  • So you mean if I write fmt.Println(time.Now().Format("2006-01-02 15:04:05-07:00")) it means different from fmt.Println(time.Now().Format("2007-01-02 15:04:05-07:00")) – Tahseen Apr 02 '17 at 02:51

1 Answers1

7

From https://golang.org/pkg/time/:

func (Time) Format

func (t Time) Format(layout string) string

Format returns a textual representation of the time value formatted according to layout, which defines the format by showing how the reference time, defined to be

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

would be displayed if it were the value;

So you must use the reference time. You should not change it to another time.

cshu
  • 5,654
  • 28
  • 44