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.

2
  • 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. play.golang.com/p/ndO8Vf1MkIQ Commented Apr 30, 2020 at 6:12
  • you are absolutely right :) thanks mkoprita Commented Apr 30, 2020 at 6:16

1 Answer 1

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          |
--------------- + ------------ +
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.