javascript - 如何在 jquery ui datepicker 和 moment.js 使用的日期格式之间进行转换?

标签 javascript .net typescript jquery-ui-datepicker momentjs

<分区>

日期格式化字符串在 jQuery UI datepicker、moment.js、.Net 的 DateTime 类中有不同的规则。如何在这些不同格式之间进行转换?

更新: 现在在 github 上:https://github.com/deerchao/dateformats

最佳答案

像这样使用 TypeScript 版本:

var j = dateFormat.convert('YYYY-MM-DD', dateFormat.momentJs, dateFormat.datepicker);

这是 TypeScript 版本:

module dateFormat {
    export interface formatRules {
        DayOfMonthShort : string;
        DayOfMonthLong : string;
        DayOfWeekShort : string;
        DayOfWeekLong : string;
        DayOfYearShort : string;
        DayOfYearLong : string;
        MonthOfYearShort : string;
        MonthOfYearLong : string;
        MonthNameShort : string;
        MonthNameLong : string;
        YearShort : string;
        YearLong : string;
        AmPm : string;
        Hour24Short : string;
        Hour24Long : string;
        Hour12Short : string;
        Hour12Long : string;
        MinuteShort : string;
        MinuteLong : string;
        SecondShort : string;
        SecondLong : string;
        FractionalSecond1 : string;
        FractionalSecond2 : string;
        FractionalSecond3 : string;
        TimeZone : string;
        UnixTimestamp : string;

        MakeLiteral(literal: string): string;
        ReadEscapedPart(format: string, startIndex: number) : escapedPart;
    }

    export interface escapedPart {
        value: string;
        length: number;
    }

    interface tokenLocateResult {
        nextBegin: number;
        literal: string;
        token: string;
    }

    export function convert(format: string, sourceRules: formatRules, destRules: formatRules) {
        if(sourceRules == destRules)
            return format;

        var result = '';
        var index = 0;
        var destTokens = getTokens(destRules);
        var sourceMap = getTokenMap(getTokens(sourceRules));
        while(index < format.length) {
            var part = locateNextToken(sourceRules, format, index);
            if(part.literal.length > 0)
                result += destRules.MakeLiteral(part.literal);
            if(part.token.length > 0)
                result += destTokens[sourceMap[part.token]];
            index = part.nextBegin;
        }

        return result;
    }

    function locateNextToken(rules: formatRules, format: string, begin: number) {
        var literal = '';
        var index = begin;
        var sequence = getTokenSequence(getTokenMap(getTokens(rules)));
        while(index < format.length) {
            var escaped = rules.ReadEscapedPart(format, index);
            if(escaped.length > 0) {
                literal += escaped.value;
                index += escaped.length;
                continue;
            }

            var token = sequence.first(x => format.indexOf(x, index) == index);
            if(!token) {
                literal += format.charAt(index);
                index++;
                continue;
            }

            return {
                token: token,
                literal: literal,
                nextBegin: index + token.length
            }
        }

        return {
            token: '',
            literal: literal,
            nextBegin: index
        }
    }

    function getTokens(rules: formatRules) {
        return [rules.DayOfMonthShort, rules. DayOfMonthLong, 
                rules.DayOfWeekShort, rules. DayOfWeekLong, 
                rules.DayOfYearShort, rules. DayOfYearLong, 
                rules.MonthOfYearShort, rules. MonthOfYearLong, 
                rules.MonthNameShort, rules. MonthNameLong, 
                rules.YearShort, rules. YearLong, 
                rules.AmPm, 
                rules.Hour24Short, rules. Hour24Long, 
                rules.Hour12Short, rules. Hour12Long, 
                rules.MinuteShort, rules. MinuteLong, 
                rules.SecondShort, rules. SecondLong, 
                rules.FractionalSecond1, rules. FractionalSecond2, rules. FractionalSecond3, 
                rules.TimeZone, 
                rules.UnixTimestamp
            ].map(x => x || '');
    }

    function getTokenMap(tokens: string[]) {
        var map = {};
        for(var i=0; i<tokens.length; i++) {
            var token = tokens[i];
            if(token) {
                map[token] = i;
            }
        }
        return map;
    }

    function getTokenSequence(map: any) {
        var tokens = Object.keys(map);
        tokens.sort((a, b) => b.length - a.length);
        return tokens;
    }

    function indexOfAny(s: string, chars: string) {
        for(var i=0; i<s.length; i++) {
            var c = s.charAt(i);
            for(var j=0; j<chars.length; j++) {
                if(c === chars.charAt(j))
                    return i;
            }
        }
        return -1;
    }

    export var standard : formatRules = {
        DayOfMonthShort : 'd',
        DayOfMonthLong : 'dd',
        DayOfWeekShort : 'ddd',
        DayOfWeekLong : 'dddd',
        DayOfYearShort : 'D',
        DayOfYearLong : 'DD',
        MonthOfYearShort : 'M',
        MonthOfYearLong : 'MM',
        MonthNameShort : 'MMM',
        MonthNameLong : 'MMMM',
        YearShort : 'yy',
        YearLong : 'yyyy',
        AmPm : 'tt',
        Hour24Short : 'H',
        Hour24Long : 'HH',
        Hour12Short : 'h',
        Hour12Long : 'hh',
        MinuteShort : 'm',
        MinuteLong : 'mm',
        SecondShort : 's',
        SecondLong : 'ss',
        FractionalSecond1 : 'f',
        FractionalSecond2 : 'ff',
        FractionalSecond3 : 'fff',
        TimeZone : 'Z',
        UnixTimestamp : 'X',

        MakeLiteral: function(literal: string) {
            var reserved = 'dDMytHhmsfZX';
            if(indexOfAny(literal, reserved) < 0)
                return literal;

            var result = '';
            for(var i=0; i< literal.length; i++) {
                var c = literal.charAt(i);
                if(reserved.contains(c))
                    result += '\\';
                result += c;
            }
            return result;
        },
        ReadEscapedPart: function(format: string, startIndex: number) {
            var result = '';
            var index = startIndex;
            while(index < format.length) {
                var c = format.charAt(index);

                if(c == '\\') {
                    result += index == format.length - 1 ? '\\' : format[++index];
                    index++;
                    continue;
                } 
                break;
            }

            return {
                value: result,
                length: index - startIndex
            }
        },
    }

    export var dotNet : formatRules = {
        DayOfMonthShort : 'd',
        DayOfMonthLong : 'dd',
        DayOfWeekShort : 'ddd',
        DayOfWeekLong : 'dddd',
        DayOfYearShort : null,
        DayOfYearLong : null,
        MonthOfYearShort : 'M',
        MonthOfYearLong : 'MM',
        MonthNameShort : 'MMM',
        MonthNameLong : 'MMMM',
        YearShort : 'yy',
        YearLong : 'yyyy',
        AmPm : 'tt',
        Hour24Short : 'H',
        Hour24Long : 'HH',
        Hour12Short : 'h',
        Hour12Long : 'hh',
        MinuteShort : 'm',
        MinuteLong : 'mm',
        SecondShort : 's',
        SecondLong : 'ss',
        FractionalSecond1 : 'f',
        FractionalSecond2 : 'ff',
        FractionalSecond3 : 'fff',
        TimeZone : 'zzz',
        UnixTimestamp : null,

        MakeLiteral: function(literal: string) {
            var reserved = 'dfFghHKmMstyz\'"';
            if(indexOfAny(literal, reserved) < 0)
                return literal;

            var result = '';
            for(var i=0; i< literal.length; i++) {
                var c = literal.charAt(i);
                if(reserved.contains(c))
                    result += '\\';
                result += c;
            }
            return result;
        },
        ReadEscapedPart: function(format: string, startIndex: number) {
            var result = '';
            var index = startIndex;
            while(index < format.length) {
                var c = format.charAt(index);

                if(c == '\\') {
                    result += index == format.length - 1 ? '\\' : format[++index];
                    index++;
                    continue;
                }

                if(c == '"') {
                    while(++index < format.length) {
                        var cc = format.charAt(index);
                        if(cc == '"')
                            break;

                        if(cc == '\\') {
                            result += index == format.length - 1 ? '\\' : format[++index];
                        } else {
                            result += cc;
                        }
                    }
                    index++;
                    continue;
                }

                if(c == "'") {
                    while(++index < format.length) {
                        var cc = format.charAt(index);
                        if(cc == "'")
                            break;

                        if(cc == '\\') {
                            result += index == format.length - 1 ? '\\' : format[++index];
                        } else {
                            result += cc;
                        }
                    }
                    index++;
                    continue;
                }

                break;
            }

            return {
                value: result,
                length: index - startIndex
            }
        },
    }

    export var momentJs : formatRules = {
        DayOfMonthShort : 'D',
        DayOfMonthLong : 'DD',
        DayOfWeekShort : 'ddd',
        DayOfWeekLong : 'dddd',
        DayOfYearShort : 'DDD',
        DayOfYearLong : 'DDDD',
        MonthOfYearShort : 'M',
        MonthOfYearLong : 'MM',
        MonthNameShort : 'MMM',
        MonthNameLong : 'MMMM',
        YearShort : 'YY',
        YearLong : 'YYYY',
        AmPm : 'A',
        Hour24Short : 'H',
        Hour24Long : 'HH',
        Hour12Short : 'h',
        Hour12Long : 'hh',
        MinuteShort : 'm',
        MinuteLong : 'mm',
        SecondShort : 's',
        SecondLong : 'ss',
        FractionalSecond1 : 'S',
        FractionalSecond2 : 'SS',
        FractionalSecond3 : 'SSS',
        TimeZone : 'Z',
        UnixTimestamp : 'X',

        MakeLiteral: function(literal: string) {
            var reserved = 'MoDdeEwWYgGAaHhmsSzZX';

            literal = literal.replaceAll("[", "(").replaceAll("]", ")");
            if(indexOfAny(literal, reserved) < 0)
                return literal;

            return '[' + literal + ']';
        },
        ReadEscapedPart: function(format: string, startIndex: number) {
            if(format.charAt(startIndex) != '[')
                return {value: '', length: 0};

            var result = '';
            var index = startIndex;
            while(index < format.length) {
                var c = format.charAt(index);

                if(c == ']') {
                    break;
                } 

                result += c;
            }

            return {
                value: result,
                length: index - startIndex
            }
        },
    }

    export var datepicker : formatRules = {
        DayOfMonthShort : 'd',
        DayOfMonthLong : 'dd',
        DayOfWeekShort : 'D',
        DayOfWeekLong : 'DD',
        DayOfYearShort : 'o',
        DayOfYearLong : 'oo',
        MonthOfYearShort : 'm',
        MonthOfYearLong : 'mm',
        MonthNameShort : 'M',
        MonthNameLong : 'MM',
        YearShort : 'y',
        YearLong : 'yy',
        AmPm : null,
        Hour24Short : null,
        Hour24Long : null,
        Hour12Short : null,
        Hour12Long : null,
        MinuteShort : null,
        MinuteLong : null,
        SecondShort : null,
        SecondLong : null,
        FractionalSecond1 : null,
        FractionalSecond2 : null,
        FractionalSecond3 : null,
        TimeZone : null,
        UnixTimestamp : '@',

        MakeLiteral: function(literal: string) {
            var reserved = "dDomMy@'";
            if(indexOfAny(literal, reserved) < 0)
                return literal;

            return "'" + literal.replaceAll("'", "''") + "'";
        },
        ReadEscapedPart: function(format: string, startIndex: number) {
            if(format.charAt(startIndex) != "'")
                return {value: '', length: 0};

            var result = '';
            var index = startIndex;
            while(++index < format.length) {
                var c = format.charAt(index);

                if(c == "'") {
                    index++;
                    if(index == format.length)
                        break;

                    if(format[index] == "'") {
                        result += c;
                    } else {
                        break;
                    }
                } else {
                    result += c;
                }
            }

            return {
                value: result,
                length: index - startIndex
            }
        },
    }

    export var timepicker : formatRules = {
        DayOfMonthShort : null,
        DayOfMonthLong : null,
        DayOfWeekShort : null,
        DayOfWeekLong : null,
        DayOfYearShort : null,
        DayOfYearLong : null,
        MonthOfYearShort : null,
        MonthOfYearLong : null,
        MonthNameShort : null,
        MonthNameLong : null,
        YearShort : null,
        YearLong : null,
        AmPm : 'TT',
        Hour24Short : 'H',
        Hour24Long : 'HH',
        Hour12Short : 'h',
        Hour12Long : 'hh',
        MinuteShort : 'm',
        MinuteLong : 'mm',
        SecondShort : 's',
        SecondLong : 'ss',
        FractionalSecond1 : null,
        FractionalSecond2 : null,
        FractionalSecond3 : 'l',
        TimeZone : 'Z',
        UnixTimestamp : null,

        MakeLiteral: function(literal: string) {
            var reserved = "HhmslctTzZ'";
            if(indexOfAny(literal, reserved) < 0)
                return literal;

            return "'" + literal.replaceAll("'", '"') + "'";
        },
        ReadEscapedPart: function(format: string, startIndex: number) {
            if(format.charAt(startIndex) != "'")
                return {value: '', length: 0};

            var result = '';
            var index = startIndex;
            while(++index < format.length) {
                var c = format.charAt(index);

                if(c == "'") {
                    index++;
                    if(index == format.length)
                        break;

                    if(format.charAt(index) == "'")
                        result += c;
                    else
                        break;
                } else {
                    result += c;
                }
            }

            return {
                value: result,
                length: index - startIndex
            }
        },
    }
}

interface String {
    replaceAll(search: string, replacement: string): string;
    contains(part: string): boolean;
}

interface Array<T> {
    first(callbackfn?: (value: T) => boolean, thisArg?: any): T;
}

和 JavaScript 版本:

var dateFormat;
(function (dateFormat) {
    function convert(format, sourceRules, destRules) {
        if (sourceRules == destRules)
            return format;

        var result = '';
        var index = 0;
        var destTokens = getTokens(destRules);
        var sourceMap = getTokenMap(getTokens(sourceRules));
        while (index < format.length) {
            var part = locateNextToken(sourceRules, format, index);
            if (part.literal.length > 0)
                result += destRules.MakeLiteral(part.literal);
            if (part.token.length > 0)
                result += destTokens[sourceMap[part.token]];
            index = part.nextBegin;
        }

        return result;
    }
    dateFormat.convert = convert;

    function locateNextToken(rules, format, begin) {
        var literal = '';
        var index = begin;
        var sequence = getTokenSequence(getTokenMap(getTokens(rules)));
        while (index < format.length) {
            var escaped = rules.ReadEscapedPart(format, index);
            if (escaped.length > 0) {
                literal += escaped.value;
                index += escaped.length;
                continue;
            }

            var token = sequence.first(function (x) {
                return format.indexOf(x, index) == index;
            });
            if (!token) {
                literal += format.charAt(index);
                index++;
                continue;
            }

            return {
                token: token,
                literal: literal,
                nextBegin: index + token.length
            };
        }

        return {
            token: '',
            literal: literal,
            nextBegin: index
        };
    }

    function getTokens(rules) {
        return [
            rules.DayOfMonthShort,
            rules.DayOfMonthLong,
            rules.DayOfWeekShort,
            rules.DayOfWeekLong,
            rules.DayOfYearShort,
            rules.DayOfYearLong,
            rules.MonthOfYearShort,
            rules.MonthOfYearLong,
            rules.MonthNameShort,
            rules.MonthNameLong,
            rules.YearShort,
            rules.YearLong,
            rules.AmPm,
            rules.Hour24Short,
            rules.Hour24Long,
            rules.Hour12Short,
            rules.Hour12Long,
            rules.MinuteShort,
            rules.MinuteLong,
            rules.SecondShort,
            rules.SecondLong,
            rules.FractionalSecond1,
            rules.FractionalSecond2,
            rules.FractionalSecond3,
            rules.TimeZone,
            rules.UnixTimestamp
        ].map(function (x) {
            return x || '';
        });
    }

    function getTokenMap(tokens) {
        var map = {};
        for (var i = 0; i < tokens.length; i++) {
            var token = tokens[i];
            if (token) {
                map[token] = i;
            }
        }
        return map;
    }

    function getTokenSequence(map) {
        var tokens = Object.keys(map);
        tokens.sort(function (a, b) {
            return b.length - a.length;
        });
        return tokens;
    }

    function indexOfAny(s, chars) {
        for (var i = 0; i < s.length; i++) {
            var c = s.charAt(i);
            for (var j = 0; j < chars.length; j++) {
                if (c === chars.charAt(j))
                    return i;
            }
        }
        return -1;
    }

    dateFormat.standard = {
        DayOfMonthShort: 'd',
        DayOfMonthLong: 'dd',
        DayOfWeekShort: 'ddd',
        DayOfWeekLong: 'dddd',
        DayOfYearShort: 'D',
        DayOfYearLong: 'DD',
        MonthOfYearShort: 'M',
        MonthOfYearLong: 'MM',
        MonthNameShort: 'MMM',
        MonthNameLong: 'MMMM',
        YearShort: 'yy',
        YearLong: 'yyyy',
        AmPm: 'tt',
        Hour24Short: 'H',
        Hour24Long: 'HH',
        Hour12Short: 'h',
        Hour12Long: 'hh',
        MinuteShort: 'm',
        MinuteLong: 'mm',
        SecondShort: 's',
        SecondLong: 'ss',
        FractionalSecond1: 'f',
        FractionalSecond2: 'ff',
        FractionalSecond3: 'fff',
        TimeZone: 'Z',
        UnixTimestamp: 'X',
        MakeLiteral: function (literal) {
            var reserved = 'dDMytHhmsfZX';
            if (indexOfAny(literal, reserved) < 0)
                return literal;

            var result = '';
            for (var i = 0; i < literal.length; i++) {
                var c = literal.charAt(i);
                if (reserved.contains(c))
                    result += '\\';
                result += c;
            }
            return result;
        },
        ReadEscapedPart: function (format, startIndex) {
            var result = '';
            var index = startIndex;
            while (index < format.length) {
                var c = format.charAt(index);

                if (c == '\\') {
                    result += index == format.length - 1 ? '\\' : format[++index];
                    index++;
                    continue;
                }
                break;
            }

            return {
                value: result,
                length: index - startIndex
            };
        }
    };

    dateFormat.dotNet = {
        DayOfMonthShort: 'd',
        DayOfMonthLong: 'dd',
        DayOfWeekShort: 'ddd',
        DayOfWeekLong: 'dddd',
        DayOfYearShort: null,
        DayOfYearLong: null,
        MonthOfYearShort: 'M',
        MonthOfYearLong: 'MM',
        MonthNameShort: 'MMM',
        MonthNameLong: 'MMMM',
        YearShort: 'yy',
        YearLong: 'yyyy',
        AmPm: 'tt',
        Hour24Short: 'H',
        Hour24Long: 'HH',
        Hour12Short: 'h',
        Hour12Long: 'hh',
        MinuteShort: 'm',
        MinuteLong: 'mm',
        SecondShort: 's',
        SecondLong: 'ss',
        FractionalSecond1: 'f',
        FractionalSecond2: 'ff',
        FractionalSecond3: 'fff',
        TimeZone: 'zzz',
        UnixTimestamp: null,
        MakeLiteral: function (literal) {
            var reserved = 'dfFghHKmMstyz\'"';
            if (indexOfAny(literal, reserved) < 0)
                return literal;

            var result = '';
            for (var i = 0; i < literal.length; i++) {
                var c = literal.charAt(i);
                if (reserved.contains(c))
                    result += '\\';
                result += c;
            }
            return result;
        },
        ReadEscapedPart: function (format, startIndex) {
            var result = '';
            var index = startIndex;
            while (index < format.length) {
                var c = format.charAt(index);

                if (c == '\\') {
                    result += index == format.length - 1 ? '\\' : format[++index];
                    index++;
                    continue;
                }

                if (c == '"') {
                    while (++index < format.length) {
                        var cc = format.charAt(index);
                        if (cc == '"')
                            break;

                        if (cc == '\\') {
                            result += index == format.length - 1 ? '\\' : format[++index];
                        } else {
                            result += cc;
                        }
                    }
                    index++;
                    continue;
                }

                if (c == "'") {
                    while (++index < format.length) {
                        var cc = format.charAt(index);
                        if (cc == "'")
                            break;

                        if (cc == '\\') {
                            result += index == format.length - 1 ? '\\' : format[++index];
                        } else {
                            result += cc;
                        }
                    }
                    index++;
                    continue;
                }

                break;
            }

            return {
                value: result,
                length: index - startIndex
            };
        }
    };

    dateFormat.momentJs = {
        DayOfMonthShort: 'D',
        DayOfMonthLong: 'DD',
        DayOfWeekShort: 'ddd',
        DayOfWeekLong: 'dddd',
        DayOfYearShort: 'DDD',
        DayOfYearLong: 'DDDD',
        MonthOfYearShort: 'M',
        MonthOfYearLong: 'MM',
        MonthNameShort: 'MMM',
        MonthNameLong: 'MMMM',
        YearShort: 'YY',
        YearLong: 'YYYY',
        AmPm: 'A',
        Hour24Short: 'H',
        Hour24Long: 'HH',
        Hour12Short: 'h',
        Hour12Long: 'hh',
        MinuteShort: 'm',
        MinuteLong: 'mm',
        SecondShort: 's',
        SecondLong: 'ss',
        FractionalSecond1: 'S',
        FractionalSecond2: 'SS',
        FractionalSecond3: 'SSS',
        TimeZone: 'Z',
        UnixTimestamp: 'X',
        MakeLiteral: function (literal) {
            var reserved = 'MoDdeEwWYgGAaHhmsSzZX';

            literal = literal.replaceAll("[", "(").replaceAll("]", ")");
            if (indexOfAny(literal, reserved) < 0)
                return literal;

            return '[' + literal + ']';
        },
        ReadEscapedPart: function (format, startIndex) {
            if (format.charAt(startIndex) != '[')
                return { value: '', length: 0 };

            var result = '';
            var index = startIndex;
            while (index < format.length) {
                var c = format.charAt(index);

                if (c == ']') {
                    break;
                }

                result += c;
            }

            return {
                value: result,
                length: index - startIndex
            };
        }
    };

    dateFormat.datepicker = {
        DayOfMonthShort: 'd',
        DayOfMonthLong: 'dd',
        DayOfWeekShort: 'D',
        DayOfWeekLong: 'DD',
        DayOfYearShort: 'o',
        DayOfYearLong: 'oo',
        MonthOfYearShort: 'm',
        MonthOfYearLong: 'mm',
        MonthNameShort: 'M',
        MonthNameLong: 'MM',
        YearShort: 'y',
        YearLong: 'yy',
        AmPm: null,
        Hour24Short: null,
        Hour24Long: null,
        Hour12Short: null,
        Hour12Long: null,
        MinuteShort: null,
        MinuteLong: null,
        SecondShort: null,
        SecondLong: null,
        FractionalSecond1: null,
        FractionalSecond2: null,
        FractionalSecond3: null,
        TimeZone: null,
        UnixTimestamp: '@',
        MakeLiteral: function (literal) {
            var reserved = "dDomMy@'";
            if (indexOfAny(literal, reserved) < 0)
                return literal;

            return "'" + literal.replaceAll("'", "''") + "'";
        },
        ReadEscapedPart: function (format, startIndex) {
            if (format.charAt(startIndex) != "'")
                return { value: '', length: 0 };

            var result = '';
            var index = startIndex;
            while (++index < format.length) {
                var c = format.charAt(index);

                if (c == "'") {
                    index++;
                    if (index == format.length)
                        break;

                    if (format[index] == "'") {
                        result += c;
                    } else {
                        break;
                    }
                } else {
                    result += c;
                }
            }

            return {
                value: result,
                length: index - startIndex
            };
        }
    };

    dateFormat.timepicker = {
        DayOfMonthShort: null,
        DayOfMonthLong: null,
        DayOfWeekShort: null,
        DayOfWeekLong: null,
        DayOfYearShort: null,
        DayOfYearLong: null,
        MonthOfYearShort: null,
        MonthOfYearLong: null,
        MonthNameShort: null,
        MonthNameLong: null,
        YearShort: null,
        YearLong: null,
        AmPm: 'TT',
        Hour24Short: 'H',
        Hour24Long: 'HH',
        Hour12Short: 'h',
        Hour12Long: 'hh',
        MinuteShort: 'm',
        MinuteLong: 'mm',
        SecondShort: 's',
        SecondLong: 'ss',
        FractionalSecond1: null,
        FractionalSecond2: null,
        FractionalSecond3: 'l',
        TimeZone: 'Z',
        UnixTimestamp: null,
        MakeLiteral: function (literal) {
            var reserved = "HhmslctTzZ'";
            if (indexOfAny(literal, reserved) < 0)
                return literal;

            return "'" + literal.replaceAll("'", '"') + "'";
        },
        ReadEscapedPart: function (format, startIndex) {
            if (format.charAt(startIndex) != "'")
                return { value: '', length: 0 };

            var result = '';
            var index = startIndex;
            while (++index < format.length) {
                var c = format.charAt(index);

                if (c == "'") {
                    index++;
                    if (index == format.length)
                        break;

                    if (format.charAt(index) == "'")
                        result += c;
else
                        break;
                } else {
                    result += c;
                }
            }

            return {
                value: result,
                length: index - startIndex
            };
        }
    };
})(dateFormat || (dateFormat = {}));

下面是字符串和数组的一些实用方法:

if (!String.prototype.replaceAll) {
    String.prototype.replaceAll = function (pattern, replacement) {
        return this.split(pattern).join(replacement);
    };
}
if (!String.prototype.contains) {
    String.prototype.contains = function (part) {
        return this.indexOf(part) >= 0;
    };
}
if (!Array.prototype.first) {
    Array.prototype.first = function (callback) {
        if (!callback)
            return this.length ? this[0] : null;

        for (var i = 0; i < this.length; i++) {
            var item = this[i];
            if (callback(item)) {
                return item;
            }
        }

        return null;
    };
}

关于javascript - 如何在 jquery ui datepicker 和 moment.js 使用的日期格式之间进行转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20101603/

相关文章:

c# - 减少代码行会减少项目的加载时间吗?

.net - 如何获得winform全路径?

javascript - 如何指示 JSON.stringify() 自定义序列化日期类型?

angular - 无法在 Angular 8 中编译 Ivy 渲染引擎

javascript - 如何让 React.js 输出有效的 xml 而不是 html?

javascript - 检测来自子级(iframe)的窗口关闭命令

.net - 使用 log4net 发送日志文件的最佳方法

javascript - javascript函数中的数据切换

javascript - 事件导航项

javascript - 在 UI 中更改复选框状态时保持不变