javascript端转换.net DateTime时间的代码
建议可以做成类的形式,不需要引入sencha-touch的js文件
var isDefined=function(v){
return typeof v !== 'undefined';
};
var useStrict = false;
var parseDate=function(input, format, strict) {
var p = parseFunctions;
if (p[format] == null) {
createParser(format);
}
return p[format](input, isDefined(strict) ? strict:useStrict);
};
var parseFunctions={
"M{1}quot;: function(input, strict) {
// note: the timezone offset is ignored since the M$ Ajax server sends
// a UTC milliseconds-since-Unix-epoch value (negative values are allowed)
var re = new RegExp('\\/Date\\(([-+])?(\\d+)(?:[+-]\\d{4})?\\)\\/');
var r = (input || '').match(re);
return r ? new Date(((r[1] || '') + r[2]) * 1) : null;
}
};
var parseRegexes=[];
var createParser=function() {
var code = [
"var dt, y, m, d, h, i, s, ms, o, z, zz, u, v,",
"def = Date.defaults,",
"results = String(input).match(Date.parseRegexes[{0}]);",
// either null, or an array of matched strings
"if(results){",
"{1}",
"if(u != null){",
// i.e. unix time is defined
"v = new Date(u * 1000);",
// give top priority to UNIX time
"}else{",
// create Date object representing midnight of the current day;
// this will provide us with our date defaults
// (note: clearTime() handles Daylight Saving Time automatically)
"dt = (new Date()).clearTime();",
// date calculations (note: these calculations create a dependency on Ext.num())
"y = Ext.num(y, Ext.num(def.y, dt.getFullYear()));",
"m = Ext.num(m, Ext.num(def.m - 1, dt.getMonth()));",
"d = Ext.num(d, Ext.num(def.d, dt.getDate()));",
// time calculations (note: these calculations create a dependency on Ext.num())
"h = Ext.num(h, Ext.num(def.h, dt.getHours()));",
"i = Ext.num(i, Ext.num(def.i, dt.getMinutes()));",
"s = Ext.num(s, Ext.num(def.s, dt.getSeconds()));",
"ms = Ext.num(ms, Ext.num(def.ms, dt.getMilliseconds()));",
"if(z >= 0 && y >= 0){",
// both the year and zero-based day of year are defined and >= 0.
// these 2 values alone provide sufficient info to create a full date object
// create Date object representing January 1st for the given year
"v = new Date(y, 0, 1, h, i, s, ms);",
// then add day of year, checking for Date "rollover" if necessary
"v = !strict? v : (strict === true && (z <= 364 || (v.isLeapYear() && z <= 365))? v.add(Date.DAY, z) : null);",
"}else if(strict === true && !Date.isValid(y, m + 1, d, h, i, s, ms)){",
// check for Date "rollover"
"v = null;",
// invalid date, so return null
"}else{",
// plain old Date object
"v = new Date(y, m, d, h, i, s, ms);",
"}",
"}",
"}",
"if(v){",
// favour UTC offset over GMT offset
"if(zz != null){",
// reset to UTC, then add offset
"v = v.add(Date.SECOND, -v.getTimezoneOffset() * 60 - zz);",
"}else if(o){",
// reset to GMT, then add offset
"v = v.add(Date.MINUTE, -v.getTimezoneOffset() + (sn == '+'? -1 : 1) * (hr * 60 + mn));",
"}",
"}",
"return v;"
].join('\n');
return function(format) {
var regexNum = Date.parseRegexes.length,
currentGroup = 1,
calc = [],
regex = [],
special = false,
ch = "",
i = 0,
obj,
last;
for (; i < format.length; ++i) {
ch = format.charAt(i);
if (!special && ch == "\\") {
special = true;
} else if (special) {
special = false;
regex.push(Ext.util.Format.escape(ch));
} else {
obj = $f(ch, currentGroup);
currentGroup += obj.g;
regex.push(obj.s);
if (obj.g && obj.c) {
if (obj.last) {
last = obj;
} else {
calc.push(obj.c);
}
}
}
}
if (last) {
calc.push(last);
}
Date.parseRegexes[regexNum] = new RegExp("^" + regex.join('') + "{1}quot;);
Date.parseFunctions[format] = new Function("input", "strict", xf(code, regexNum, calc.join('')));
};
}();
function formatDate(dateString) {
var ticks=parseDate(dateString, "M{1}quot;);
d = new Date(ticks);
return d.getDate() + "/"+
(d.getMonth() + 1) + "/" +
d.getFullYear().toString().substring(2,4)
};