1 |
|
2 |
|
3 |
|
4 |
|
5 | 'use strict';
|
6 |
|
7 |
|
8 |
|
9 | module.exports.sortByDate = function (list) {
|
10 | if (!list) throw new Error('List is not given');
|
11 | if (list.length === 1) return;
|
12 |
|
13 | list.sort(function (a, b) {
|
14 | if (!a.stat || !b.stat) throw new Error('List has no `stat` property');
|
15 | if (!a.stat.mtime || !b.stat.mtime) throw new Error('List has no `mtime` property');
|
16 |
|
17 | let keyA = new Date(a.stat.mtime),
|
18 | keyB = new Date(b.stat.mtime);
|
19 |
|
20 |
|
21 | if (keyA < keyB) return 1;
|
22 | if (keyA > keyB) return -1;
|
23 | return 0;
|
24 | });
|
25 | return list;
|
26 | };
|
27 |
|
28 |
|
29 | module.exports.parseDate = function (str) {
|
30 | let date = new Date(str);
|
31 |
|
32 |
|
33 | if (isNaN(date.getTime())) throw new Error('Invalid Date');
|
34 |
|
35 | if (date.getFullYear() == new Date().getFullYear()) {
|
36 |
|
37 | return `${date.getMonth() + 1}-${date.getDate()} ${date.getHours()}:${date.getMinutes()}`;
|
38 | } else {
|
39 | return `${date.getMonth() + 1}-${date.getDate()} ${date.getHours()}:${date.getMinutes()}, ${date.getFullYear()}`;
|
40 | }
|
41 | }; |
\ | No newline at end of file |