Format JavaScript date as yyyy-mm-dd

后端 未结 30 2684
再見小時候
再見小時候 2020-11-22 01:28

I have a date with the format Sun May 11,2014. How can I convert it to 2014-05-11 using JavaScript?

30条回答
  •  不知归路
    2020-11-22 02:10

    A few of the previous answer were OK, but they weren't very flexible. I wanted something that could really handle more edge cases, so I took @orangleliu 's answer and expanded on it. https://jsfiddle.net/8904cmLd/1/

    function DateToString(inDate, formatString) {
        // Written by m1m1k 2018-04-05
    
        // Validate that we're working with a date
        if(!isValidDate(inDate))
        {
            inDate = new Date(inDate);
        }
    
        // See the jsFiddle for extra code to be able to use DateToString('Sun May 11,2014', 'USA');
        //formatString = CountryCodeToDateFormat(formatString);
    
        var dateObject = {
            M: inDate.getMonth() + 1,
            d: inDate.getDate(),
            D: inDate.getDate(),
            h: inDate.getHours(),
            m: inDate.getMinutes(),
            s: inDate.getSeconds(),
            y: inDate.getFullYear(),
            Y: inDate.getFullYear()
        };
    
        // Build Regex Dynamically based on the list above.
        // It should end up with something like this: "/([Yy]+|M+|[Dd]+|h+|m+|s+)/g"
        var dateMatchRegex = joinObj(dateObject, "+|") + "+";
        var regEx = new RegExp(dateMatchRegex,"g");
        formatString = formatString.replace(regEx, function(formatToken) {
            var datePartValue = dateObject[formatToken.slice(-1)];
            var tokenLength = formatToken.length;
    
            // A conflict exists between specifying 'd' for no zero pad -> expand
            // to '10' and specifying yy for just two year digits '01' instead
            // of '2001'.  One expands, the other contracts.
            //
            // So Constrict Years but Expand All Else
            if (formatToken.indexOf('y') < 0 && formatToken.indexOf('Y') < 0)
            {
                // Expand single digit format token 'd' to
                // multi digit value '10' when needed
                var tokenLength = Math.max(formatToken.length, datePartValue.toString().length);
            }
            var zeroPad = (datePartValue.toString().length < formatToken.length ? "0".repeat(tokenLength) : "");
            return (zeroPad + datePartValue).slice(-tokenLength);
        });
    
        return formatString;
    }
    

    Example usage:

    DateToString('Sun May 11,2014', 'MM/DD/yy');
    DateToString('Sun May 11,2014', 'yyyy.MM.dd');
    DateToString(new Date('Sun Dec 11,2014'),'yy-M-d');
    

提交回复
热议问题