-2

I've tried to get the time in desired format like below. Below code doesn't seem to be working. This is also the case if I use RFC3339 layout. Any help is appreciated.

stamp, err := time.Parse("2006-01-02T15:04:05.000000Z", "2020-05-21T23:22:14.45321Z")
log.Println(stamp.Format("September 01, 2020"))
Santhosh
  • 130
  • 1
  • 2
  • 11
  • "Doesn't seem to be working" isn't a problem statement. WHAT doesn't work? Do you get an error, or some other unexpected behavior? – Jonathan Hall Jan 27 '21 at 08:27
  • I believe, the intent is quite clear from the 2 statements alone, date formatting is not working as expected. – Santhosh Jan 27 '21 at 10:24
  • Once again: "not working as expected" isn't a problem statement. What doesn't work? Do you get an error, or some other unexpected behavior? The _intent_ may be clear, but the problem is not. – Jonathan Hall Jan 27 '21 at 10:26

1 Answers1

0

There's a reference timestamp mentioned in the docs that you need to follow; otherwise, it won't work. Read: https://golang.org/pkg/time/#pkg-constants

The following code should help:

package main

import (
    "fmt"
    "os"
    "time"
)

func main() {
    // Parse the time of layout: RFC3339
    t, err := time.Parse(time.RFC3339, "2020-05-21T23:22:14.45321Z")
    if err != nil {
        panic(err)
    }
    // Reference for layout: Mon Jan 2 15:04:05 -0700 MST 2006
    // Format: Jan 02, 2006
    fmt.Fprintf(os.Stdout, "%s\n", t.Format("Jan 02, 2006"))
}
shmsr
  • 3,802
  • 2
  • 18
  • 29