Skip to content

【LeetCode】1507. 转变日期格式 #159

@WGrape

Description

@WGrape

介绍

image

题目链接 :https://leetcode-cn.com/problems/reformat-date/

解题

func reformatDate(date string) string {
    dayMap := map[string]string{
		"1st": "01",
		"2nd": "02",
		"3rd": "03",

		"4th": "04",
		"5th": "05",
		"6th": "06",
		"7th": "07",
		"8th": "08",

		"9th":  "09",
		"10th": "10",
		"11th": "11",
		"12th": "12",
		"13th": "13",

		"14th": "14",
		"15th": "15",
		"16th": "16",
		"17th": "17",
		"18th": "18",

        // 注意是21st, 22nd, 23rd, 而不是th
		"19th": "19",
		"20th": "20",
		"21st": "21",
		"22nd": "22",
		"23rd": "23",

		"24th": "24",
		"25th": "25",
		"26th": "26",
		"27th": "27",
		"28th": "28",

		"29th": "29",
		"30th": "30",
		"31st": "31",
	}

    monthMap := map[string]string{
        "Jan":"01", 
        "Feb":"02", 
        "Mar":"03", 
        "Apr":"04", 
        "May":"05", 
        "Jun":"06", 
        "Jul":"07", 
        "Aug":"08", 
        "Sep":"09", 
        "Oct":"10", 
        "Nov":"11", 
        "Dec":"12",
    }

    pieces := strings.Split(date, " ")


    formatDate := fmt.Sprintf("%s-%s-%s", pieces[2], monthMap[pieces[1]], dayMap[pieces[0]])
    return formatDate
}

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions