由网友(愿岁月伴你荒芜)分享简介:日期格式化字符串在jQuery UI的日期选择器,moment.js,从净DateTime类不同的规则。如何转换这些不同格式之间?The date formatting string has different rules in jQuery UI datepicker, moment.js, DateTime cl...
日期格式化字符串在jQuery UI的日期选择器,moment.js,从净DateTime类不同的规则。如何转换这些不同格式之间?
The date formatting string has different rules in jQuery UI datepicker, moment.js, DateTime class from .Net. How do I convert between these different formats?
推荐答案
使用的打字稿的版本是这样的:
Use the TypeScript version like this:
var j = dateFormat.convert('YYYY-MM-DD', dateFormat.momentJs, dateFormat.datepicker);
下面是打字稿版本:
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的版本:
And the JavaScript version:
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 = {}));
和这里是字符串和阵列的一些实用方法:
And here is some utility methods for string and array:
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;
};
}
相关推荐
最新文章