Open AlexZ33 opened 4 years ago
filters.timefy = function(time) {
const myDate = new Date(time * 1000);
return myDate.getFullYear() + '年' + (myDate.getMonth() + 1) + '月' + myDate.getDate() + '日';
// return moment(time).format('YYYY 年 MM 月 DD 日')
};
filters.filterTime = function(time) {
time = new Date().getTime() - time;
const between = time / 1000;
if (between < 60) {
return pluralize(1, '分钟');
} else if (between < 3600) {
return pluralize(~~(between / 60), '分钟');
} else if (between < 86400) {
return pluralize(~~(between / 3600), '小时');
} else if (between < 31536000) {
return pluralize(~~(between / 86400), '天');
} else {
return pluralize(~~(between / 31536000), '年');
}
};
filters.formatTime = function(time) {
const year = new Date(time).getFullYear();
if (isNaN(year)) return '';
let month = new Date(time).getMonth();
month = (month > 10) ? month : '0' + month;
let date = new Date(time).getDate();
date = (date > 10) ? date : '0' + date;
return year + '-' + month + '-' + date;
};
/**
* Created by Administrator on 2016/3/17.
*/
define('utils/DateUtils', [], () => {
const PARSE_CONFIG = {
shortYearCutoff: '+10',
monthNames: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
monthNamesShort: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
dayNames: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
dayNamesShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
};
const formatDate = function (format, date, settings) {
// console.log(date);
if (!date) {
return null;
}
const s = _.extend({}, PARSE_CONFIG, settings);
const look = function (m) {
let n = 0;
while (i + 1 < format.length && format.charAt(i + 1) == m) {
n++;
i++;
}
return n;
};
const f1 = function (m, val, len) {
let n = `${val}`;
if (look(m)) {
while (n.length < len) {
n = `0${n}`;
}
}
return n;
};
const f2 = function (m, val, s, l) {
return (look(m) ? l[val] : s[val]);
};
let i;
let output = '';
let literal = false;
for (i = 0; i < format.length; i++) {
if (literal) {
if (format.charAt(i) == "'" && !look("'")) {
literal = false;
} else {
output += format.charAt(i);
}
} else {
switch (format.charAt(i)) {
case 'd':
output += f1('d', date.getDate(), 2);
break;
case 'D':
output += f2('D', date.getDay(), s.dayNamesShort, s.dayNames);
break;
case 'o':
output += f1('o', (date.getTime() - new Date(date.getFullYear(), 0, 0).getTime()) / 86400000, 3);
break;
case 'm':
output += f1('m', date.getMonth() + 1, 2);
break;
case 'M':
output += f2('M', date.getMonth(), s.monthNamesShort, s.monthNames);
break;
case 'y':
output += (look('y') ? date.getFullYear() : (date.getYear() % 100 < 10 ? '0' : '') + date.getYear() % 100);
break;
case 'h':
var h = date.getHours();
output += f1('h', (h > 12 ? (h - 12) : (h == 0 ? 12 : h)), 2);
break;
case 'H':
output += f1('H', date.getHours(), 2);
break;
case 'i':
output += f1('i', date.getMinutes(), 2);
break;
case 's':
output += f1('s', date.getSeconds(), 2);
break;
case 'a':
output += date.getHours() > 11 ? 'pm' : 'am';
break;
case 'A':
output += date.getHours() > 11 ? 'PM' : 'AM';
break;
case "'":
if (look("'")) {
output += "'";
} else {
literal = true;
}
break;
default:
output += format.charAt(i);
}
}
}
return output;
};
const parseDate = function (format, value, settings) {
const def = new Date();
if (!format || !value) {
return def;
}
value = (typeof value === 'object' ? value.toString() : `${value}`);
const s = _.extend({}, PARSE_CONFIG, settings);
const { shortYearCutoff } = s;
let year = def.getFullYear();
let month = def.getMonth() + 1;
let day = def.getDate();
let doy = -1;
let hours = def.getHours();
let minutes = def.getMinutes();
let seconds = 0;
let ampm = -1;
let literal = false;
const lookAhead = function (match) {
const matches = (iFormat + 1 < format.length && format.charAt(iFormat + 1) == match);
if (matches) {
iFormat++;
}
return matches;
};
const getNumber = function (match) {
lookAhead(match);
const size = (match == '@' ? 14 : (match == '!' ? 20 : (match == 'y' ? 4 : (match == 'o' ? 3 : 2))));
const digits = new RegExp(`^\\d{1,${size}}`);
const num = value.substr(iValue).match(digits);
if (!num) {
return 0;
}
iValue += num[0].length;
return parseInt(num[0], 10);
};
const getName = function (match, s, l) {
const names = (lookAhead(match) ? l : s);
let i;
for (i = 0; i < names.length; i++) {
if (value.substr(iValue, names[i].length).toLowerCase() == names[i].toLowerCase()) {
iValue += names[i].length;
return i + 1;
}
}
return 0;
};
const checkLiteral = function () {
iValue++;
};
var iValue = 0;
let iFormat;
for (iFormat = 0; iFormat < format.length; iFormat++) {
if (literal) {
if (format.charAt(iFormat) == "'" && !lookAhead("'")) {
literal = false;
} else {
checkLiteral();
}
} else {
switch (format.charAt(iFormat)) {
case 'd':
day = getNumber('d');
break;
case 'D':
getName('D', s.dayNamesShort, s.dayNames);
break;
case 'o':
doy = getNumber('o');
break;
case 'm':
month = getNumber('m');
break;
case 'M':
month = getName('M', s.monthNamesShort, s.monthNames);
break;
case 'y':
year = getNumber('y');
break;
case 'H':
hours = getNumber('H');
break;
case 'h':
hours = getNumber('h');
break;
case 'i':
minutes = getNumber('i');
break;
case 's':
seconds = getNumber('s');
break;
case 'a':
ampm = getName('a', ['am', 'pm'], ['am', 'pm']) - 1;
break;
case 'A':
ampm = getName('A', ['am', 'pm'], ['am', 'pm']) - 1;
break;
case "'":
if (lookAhead("'")) {
checkLiteral();
} else {
literal = true;
}
break;
default:
checkLiteral();
}
}
}
if (year < 100) {
year += new Date().getFullYear() - new Date().getFullYear() % 100
+ (year <= (typeof shortYearCutoff !== 'string' ? shortYearCutoff : new Date().getFullYear() % 100 + parseInt(shortYearCutoff, 10)) ? 0 : -100);
}
if (doy > -1) {
month = 1;
day = doy;
do {
const dim = 32 - new Date(year, month - 1, 32).getDate();
if (day <= dim) {
break;
}
month++;
day -= dim;
} while (true);
}
hours = (ampm == -1) ? hours : ((ampm && hours < 12) ? (hours + 12) : (!ampm && hours == 12 ? 0 : hours));
const date = new Date(year, month - 1, day, hours, minutes, seconds);
if (date.getFullYear() != year || date.getMonth() + 1 != month || date.getDate() != day) {
throw 'Invalid date';
}
return date;
};
return {
parseDate,
formatDate
};
});
坑