|
@@ -6,6 +6,7 @@ |
|
@@ -6,6 +6,7 @@ |
6
|
'use strict';
|
6
|
'use strict';
|
7
|
const querystring = require('querystring');
|
7
|
const querystring = require('querystring');
|
8
|
const _ = require('lodash');
|
8
|
const _ = require('lodash');
|
|
|
9
|
+const moment = require('moment');
|
9
|
const config = require('../config/common');
|
10
|
const config = require('../config/common');
|
10
|
|
11
|
|
11
|
/**
|
12
|
/**
|
|
@@ -75,3 +76,62 @@ exports.upperCase = (str) => { |
|
@@ -75,3 +76,62 @@ exports.upperCase = (str) => { |
75
|
str = str || '';
|
76
|
str = str || '';
|
76
|
return str.toUpperCase();
|
77
|
return str.toUpperCase();
|
77
|
};
|
78
|
};
|
|
|
79
|
+
|
|
|
80
|
+/**
|
|
|
81
|
+ * 时间格式化
|
|
|
82
|
+ * @param format 格式化token @see{http://momentjs.cn/docs/#/displaying/format/}
|
|
|
83
|
+ * @param date 日期或者数字
|
|
|
84
|
+ * @return string
|
|
|
85
|
+ *
|
|
|
86
|
+ */
|
|
|
87
|
+exports.dateFormat = (format, date) => {
|
|
|
88
|
+ if (typeof format !== 'string' || typeof date === 'undefined') {
|
|
|
89
|
+ return '';
|
|
|
90
|
+ } else {
|
|
|
91
|
+ if (date instanceof Date) {
|
|
|
92
|
+ return moment(date).format(format);
|
|
|
93
|
+ } else {
|
|
|
94
|
+ let d = moment.unix(date);
|
|
|
95
|
+
|
|
|
96
|
+ return moment(d).utc().format(format);
|
|
|
97
|
+ }
|
|
|
98
|
+ }
|
|
|
99
|
+};
|
|
|
100
|
+
|
|
|
101
|
+/**
|
|
|
102
|
+ * 时间差格式化
|
|
|
103
|
+ * @param {[string]} format 格式化字符串
|
|
|
104
|
+ * @param {[number]} diff 相差值
|
|
|
105
|
+ * @param {[string]} type diff时间类型 默认ms
|
|
|
106
|
+ *
|
|
|
107
|
+ * Key Shorthand
|
|
|
108
|
+ * years y
|
|
|
109
|
+ * quarters Q
|
|
|
110
|
+ * months M
|
|
|
111
|
+ * weeks w
|
|
|
112
|
+ * days d
|
|
|
113
|
+ * hours h
|
|
|
114
|
+ * minutes m
|
|
|
115
|
+ * seconds s
|
|
|
116
|
+ * milliseconds ms
|
|
|
117
|
+ *
|
|
|
118
|
+ * @example
|
|
|
119
|
+ * let diff = 60 * 60 * 24 * (1.3) + 2;
|
|
|
120
|
+ *
|
|
|
121
|
+ * let s = helpers.dateDiffFormat('{d}天{h}小时', diff, 's');
|
|
|
122
|
+ * >>> 1天7小时
|
|
|
123
|
+ */
|
|
|
124
|
+exports.dateDiffFormat = (format, diff, type) => {
|
|
|
125
|
+ if (typeof format !== 'string' || typeof diff === 'undefined') {
|
|
|
126
|
+ return '';
|
|
|
127
|
+ } else {
|
|
|
128
|
+ type = type || 'ms';
|
|
|
129
|
+ let m = moment.duration(diff, type);
|
|
|
130
|
+
|
|
|
131
|
+ format.match(/(\{.*?\})/g).forEach((s) => {
|
|
|
132
|
+ format = format.replace(s, m.get(s.substring(1, s.length - 1)));
|
|
|
133
|
+ });
|
|
|
134
|
+
|
|
|
135
|
+ return format;
|
|
|
136
|
+ }
|
|
|
137
|
+}; |