-
Notifications
You must be signed in to change notification settings - Fork 0
/
date.js
62 lines (60 loc) · 2.2 KB
/
date.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const path = require('path');
const Excel = require('../index');
let writer = Excel.createWriter({
NaN: '-'
});
const titleOpts = {
font: {
bold: true,
name: '微软雅黑',
sz: 10
}
};
const moment = require('moment')
writer.withoutGridLines().sheet('Date')
.row().cell('utc', titleOpts).width(16)
.cell('expect', titleOpts).width(16)
.cell('date', titleOpts).width(16)
.cell('expect', titleOpts).width(16);
// 数据库存储值:2019-07-31 07:18:00
// 北京日期对象:2019-07-30T23:18:00.000Z
// UTC日期对象:2019-07-31T07:18:00.000Z
// 与数据库相同:2019-07-31 07:18:00
// 北京时间预期:2019-07-31 15:18:00
// UTC时间预期:2019-07-31 07:18:00
const date = new Date('2019-07-31 07:18:00')
const utc = moment.utc(moment(date).format('YYYY-MM-DD HH:mm:ss')).utcOffset(8).toDate()
const bj = moment.utc(moment(date).format('YYYY-MM-DD HH:mm:ss')).subtract(8, 'hours').toDate()
const data = [
// 不论时区与数据库保持一致
{
v1: date,
v2: process.env.TZ === 'UTC' ? '2019-07-31 07:18:00' : '2019-07-30 23:18:00',
v3: date,
v4: '2019-07-31 07:18:00'
}, { // 数据库存储的是UTC时间,显示成北京时间
v1: utc,
v2: '2019-07-31 07:18:00',
v3: moment.utc(moment(date).format('YYYY-MM-DD HH:mm:ss')).utcOffset(8).format('YYYY-MM-DD HH:mm:ss'),
v4: '2019-07-31 15:18:00'
}, { // 数据库存储的是北京时间,显示成北京时间
v1: bj,
v2: '2019-07-30 23:18:00',
v3: date,
v4: '2019-07-31 07:18:00'
}
]
for (let d of data) {
writer.row().utc(d.v1, 'YYYY-MM-DD HH:mm:ss').cell(d.v2).date(d.v3, 'YYYY-MM-DD HH:mm:ss').cell(d.v4);
}
writer.row().row()
const d = new Date('2018-01-01T15:16:00.000Z')
writer.utc(d, 'YYYY-MM-DD HH:mm:ss').cell('2018-01-01 15:16:00')
writer.utc('2018-01-01 23:16:00').cell(process.env.TZ === 'UTC' ? '2018-01-01 23:16:00' : '2018-01-01 15:16:00')
writer.row()
writer.date(d, 'YYYY-MM-DD HH:mm:ss').cell(process.env.TZ === 'UTC' ? '2018-01-01 15:16:00' : '2018-01-01 23:16:00')
writer.date('2018-01-01 23:16:00').cell('2018-01-01 23:16:00')
writer.border2end(0, 0, '#000000');
const filename = path.resolve(__dirname, 'date.xlsx');
writer.save(filename);
console.log('saved =>', filename);