Convert to a Friendly Date Output in Golang

Published: 2024-05-12 | Updated: 2024-05-17

Here is a function which takes an input string of the format YYYY-MM-DD and converts it into a more readable “Mar 06, 2006” format

func formatDateFriendly(yymmdd string) (string, error) {
    date, err := time.Parse("2006-01-02", yymmdd)
    if err != nil {
        return "", err
    }

    month := date.Month().String()
    day := strconv.Itoa(date.Day())
    year := strconv.Itoa(date.Year())

    formattedDate := month[:3] + " " + day + ", " + year

    return formattedDate, nil
}