...
|
...
|
@@ -6,6 +6,7 @@ |
|
|
'use strict';
|
|
|
const querystring = require('querystring');
|
|
|
const _ = require('lodash');
|
|
|
const moment = require('moment');
|
|
|
const config = require('../config/common');
|
|
|
|
|
|
/**
|
...
|
...
|
@@ -75,3 +76,62 @@ exports.upperCase = (str) => { |
|
|
str = str || '';
|
|
|
return str.toUpperCase();
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
* 时间格式化
|
|
|
* @param format 格式化token @see{http://momentjs.cn/docs/#/displaying/format/}
|
|
|
* @param date 日期或者数字
|
|
|
* @return string
|
|
|
*
|
|
|
*/
|
|
|
exports.dateFormat = (format, date) => {
|
|
|
if (typeof format !== 'string' || typeof date === 'undefined') {
|
|
|
return '';
|
|
|
} else {
|
|
|
if (date instanceof Date) {
|
|
|
return moment(date).format(format);
|
|
|
} else {
|
|
|
let d = moment.unix(date);
|
|
|
|
|
|
return moment(d).utc().format(format);
|
|
|
}
|
|
|
}
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
* 时间差格式化
|
|
|
* @param {[string]} format 格式化字符串
|
|
|
* @param {[number]} diff 相差值
|
|
|
* @param {[string]} type diff时间类型 默认ms
|
|
|
*
|
|
|
* Key Shorthand
|
|
|
* years y
|
|
|
* quarters Q
|
|
|
* months M
|
|
|
* weeks w
|
|
|
* days d
|
|
|
* hours h
|
|
|
* minutes m
|
|
|
* seconds s
|
|
|
* milliseconds ms
|
|
|
*
|
|
|
* @example
|
|
|
* let diff = 60 * 60 * 24 * (1.3) + 2;
|
|
|
*
|
|
|
* let s = helpers.dateDiffFormat('{d}天{h}小时', diff, 's');
|
|
|
* >>> 1天7小时
|
|
|
*/
|
|
|
exports.dateDiffFormat = (format, diff, type) => {
|
|
|
if (typeof format !== 'string' || typeof diff === 'undefined') {
|
|
|
return '';
|
|
|
} else {
|
|
|
type = type || 'ms';
|
|
|
let m = moment.duration(diff, type);
|
|
|
|
|
|
format.match(/(\{.*?\})/g).forEach((s) => {
|
|
|
format = format.replace(s, m.get(s.substring(1, s.length - 1)));
|
|
|
});
|
|
|
|
|
|
return format;
|
|
|
}
|
|
|
}; |
...
|
...
|
|