Distinguish MM/DD/YYYY and DD/MM/YYYY formats

Hi All

I’m attempting the “Booking up for Beauty” exercise. I’m getting stuck with Go unable to parse the date in the format I want.

01/02/2023 could be parsed into different values based on the format (01-FEB-2023 in DD/MM/YYYY format or 02-JAN-2023 in MM/DD/YYYY format). Other languages provide precise format strings to distinguish between the two formats.

Given that Go uses examples to parse the dates, how does Go distinguish between MM/DD/YYYY and DD/MM/YYYY formats ?

The time package provides date parsing with the Parse() function. It uses the time.Format to figure out how to parse the string. The format string must use the exact date time Jan 2, 03:04:05pm 2006 (date 01/02/06) to specify the format. To translate from other languages, use this replacement mapping: {%m => 1; %d => 2; %H => %3; %M => 4; %S => 5; %Y => 6}.

Just to be extra explicit following Isaac’s post:

  • 01/02/2006MM/DD/YYYY
  • 02/01/2006DD/MM/YYYY

I also find Go’s datetime parsing to be odd. But the rules are very clear.

Also YYYY-MM-DD is the one true date format!

1 Like

Thanks @glennj and @IsaacG, I’ve understood it now.