0

Im trying to parse date with custom format- ddmmmyyyy. for example:

15may1997
dd 15 
mmm may 
yyyy 1997

This is my code:

const customFormat = "02jan2006"
t, err := time.Parse(customFormat, "15may1997")
if err != nil {
    fmt.Println(err)
}

And this is the output:

parsing time "15may1997" as "02jan2006": cannot parse "may1997" as "jan"

The parsing succeed only when changing the day:

t, err := time.Parse(customFormat, "15jan2006")

I have tried to read this and this and more articles, but couldn't figure out how to custom to my format.

What am I doing wrong here?

Thanks.

user1673206
  • 1,671
  • 1
  • 23
  • 43
  • 2
    *"The recognized day of week formats are "Mon" and "Monday". The recognized month formats are "Jan" and "January"."* i.e. Use `Jan` instead of `jan`. https://play.golang.com/p/ndO8Vf1MkIQ – mkopriva Apr 30 '20 at 06:12
  • you are absolutely right :) thanks mkoprita – user1673206 Apr 30 '20 at 06:16

1 Answers1

1

Golang time.Parse parses a formatted string on the basis of layout and returns the time value it represents.The layout which defines the format is Case-sensitive and has fixed predefined values.

So you need to update customFormat = "02jan2006" to "02Jan2006"

Below is the list of Case-sensitive Placeholders for different parts of the datetime.

--------------- + ------------ +
Type            | Placeholder  |
--------------- + ------------ +
Year            | 2006         |
Year            | 06           | 
Month           | 01           |
Month           | 1            |
Month           | Jan          |
Month           | January      |
Day             | 02           |
Day             | 2            |
Week day        | Mon          |
Week day        | Monday       |
Hours           | 03           |
Hours           | 3            |
Hours           | 15           |
Minutes         | 04           |
Minutes         | 4            |
Seconds         | 05           | 
Seconds         | 5            |
AM or PM        | PM           | 
Miliseconds     | .000         |
Microseconds    | .000000      |
Nanoseconds     | .000000000   |
Timezone offset | -0700        |
Timezone offset | -07:00       |   
Timezone offset | Z0700        |  
Timezone offset | Z07:00       |   
Timezone        | MST          |
--------------- + ------------ +
Puneet Singh
  • 3,477
  • 1
  • 26
  • 39