1

I have a date string Oct 10 2022 and I want to convert this to a time object. I have tried with time.Parse, but it always returns 0001-01-01 00:00:00 +0000 UTC

date := "Oct 10 2022"
output, _ := time.Parse(time.ANSIC, date)
fmt.Println(output)

How do I get a time object from the above string?

kiran
  • 444
  • 3
  • 15
  • 1
    Your input date does not comply with the ANSIC layout - https://pkg.go.dev/time#pkg-constants. Therefore you cannot get a meaningful date output from parse – Inian Oct 08 '22 at 16:53
  • Yes, is there any way to get the same? – kiran Oct 08 '22 at 16:57

1 Answers1

1

for cast your favorite time string to time.ANSIC, you must do it like below

date = "Mon Oct 10 15:04:05 2022"
output, _ := time.Parse(time.ANSIC, date)
fmt.Println(output)

other time package constants for cast like below:

    Layout      = "01/02 03:04:05PM '06 -0700" // The reference time, in numerical order.
    ANSIC       = "Mon Jan 2 15:04:05 2006"
    UnixDate    = "Mon Jan 2 15:04:05 MST 2006"
    RubyDate    = "Mon Jan 02 15:04:05 -0700 2006"
    RFC822      = "02 Jan 06 15:04 MST"
    RFC822Z     = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone
    RFC850      = "Monday, 02-Jan-06 15:04:05 MST"
    RFC1123     = "Mon, 02 Jan 2006 15:04:05 MST"
    RFC1123Z    = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone
    RFC3339     = "2006-01-02T15:04:05Z07:00"
    RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
    Kitchen     = "3:04PM"
    Stamp       = "Jan 2 15:04:05"
    StampMilli  = "Jan 2 15:04:05.000"
    StampMicro  = "Jan 2 15:04:05.000000"
    StampNano   = "Jan 2 15:04:05.000000000"