Skip to content

Commit 986b484

Browse files
feat(index.js): 更新更自由的format转入格式
1 parent 784f05e commit 986b484

File tree

2 files changed

+34
-26
lines changed

2 files changed

+34
-26
lines changed

README.md

+27-11
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,43 @@
11
a tool for transferring timestamp to standard time format
22

3-
# usage
3+
# Date
4+
5+
6+
| Name | Description | Example |
7+
| --- | --- | --- |
8+
| YYYY | Year | 2017 |
9+
| MM | Month | 02 |
10+
| DD | Date | 01 |
11+
| hh | Hour | 23 |
12+
| mm | Minute | 01 |
13+
| ss | Second | 02 |
14+
15+
Above is time-schema-formatter supported format. You can make your own time format. For example:
16+
17+
- YYYY/MM/DD hh:mm:ss | 2017/06/07 23:01:02
18+
- YYYY年MM月DD日 hh-mm-ss | 2017年06月07日 23-01-02
19+
- YYYY-MM-DD | 2017-06-07
20+
21+
# Usage
422

523
```javascript
624
import timeFormat from 'time-schema-formatter'
725

8-
const now = Date.parse(new Date())
9-
10-
// 2017-06-09
26+
// 2017/06/09 23:01:02
1127
timeFormat({
12-
time: Date.parse(new Date()),
13-
format: 'detail',
28+
time: Date.parse(new Date()),
29+
format: 'YYYY/MM/DD hh:mm:ss',
1430
});
1531

16-
// 20170609
32+
// 2017年06月09日 23-01-02
1733
timeFormat({
18-
time: Date.parse(new Date()),
19-
format: 'row',
34+
time: Date.parse(new Date()),
35+
format: 'YYYY年MM月DD日 hh-mm-ss',
2036
});
2137

2238
// 2017-06-09
2339
timeFormat({
24-
time: Date.parse(new Date()),
25-
format: 'slash',
40+
time: Date.parse(new Date()),
41+
format: 'YYYY-MM-DD',
2642
});
2743
```

index.js

+7-15
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,13 @@ function getTime(option) {
88
const min = date.getMinutes() < 10 ? `0${date.getMinutes()}` : date.getMinutes();
99
const sec = date.getSeconds() < 10 ? `0${date.getSeconds()}` : date.getSeconds();
1010

11-
let times;
12-
switch (option.format) {
13-
case 'detail':
14-
times = `${year}.${month}.${day} ${hour}:${min}:${sec}`;
15-
break;
16-
case 'row':
17-
times = `${year}${month}${day}`;
18-
break;
19-
case 'slash':
20-
times = `${year}-${month}-${day}`;
21-
break;
22-
default:
23-
times = option.time;
24-
break;
25-
}
11+
let times = option.format;
12+
times = times.replace('YYYY', year);
13+
times = times.replace('MM', month);
14+
times = times.replace('DD', day);
15+
times = times.replace('hh', hour);
16+
times = times.replace('mm', min);
17+
times = times.replace('ss', sec);
2618

2719
return times
2820

0 commit comments

Comments
 (0)