-2

I am trying to format time using time Format function as MMM dd yyyy, HH:mm:ss format but getting incorrect date as output. Below is the code for reference

package main

import (
    "fmt"
    "time"
)

func main() {
    var t time.Time
    t = time.Now()
    fmt.Println(t.Format("Jan 01 2006, 15:04:05"))
    //Output
    //Dec 12 2018, 16:27:34
}

But if I change the reference format as Jan 02 2006, 15:04:05 I get a correct output. So I am not able to find what is the issue between two dates reference and what would be the correct reference date format which works in all use case.

Abhinav
  • 975
  • 4
  • 18
  • 35
  • 1
    You get correct output when you pass correct input. It's all covered in the documentation: https://golang.org/pkg/time/#example_Time_Format – Adrian Dec 31 '18 at 14:27

1 Answers1

1

Layouts must use the reference time Mon Jan 2 15:04:05 MST 2006 to show the pattern with which to format/parse a given time/string.

Source: https://gobyexample.com/time-formatting-parsing

If you really want to understand this fully, dig into the source.

maio290
  • 6,440
  • 1
  • 21
  • 38
  • If you really want to understand this fully, read the package `time` documentation: https://golang.org/pkg/time/ – peterSO Dec 31 '18 at 14:30
  • The comments are the same as in the source ;) – maio290 Dec 31 '18 at 14:31
  • First, read the documentation. Then, if necessary, read the source. By design, unformatted documentation is included in the source. – peterSO Dec 31 '18 at 14:32
  • I'm not going to discuss with you what kind of information someone has to prefer since that's down to a personal taste. However, I think the source code is always a more detailed documentation than any documentation can ever be. Since the reason for the behaviour can be found in it. But that's just my point of view and there is no wrong or correct here. – maio290 Dec 31 '18 at 16:54