/
timestamp.vue
94 lines (91 loc) · 2.78 KB
/
timestamp.vue
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<template>
<div class="timestamp">
<nya-container title="时间戳转换">
<div v-if="chooseManually">
<client-only>
<date-picker v-model="date" type="datetime" class="nya-input date-picker mt-15" format="YYYY-MM-DD hh:mm:ss" confirm :editable="false" placeholder="选择日期" :default-value="defaultValue" value-type="timestamp" />
</client-only>
</div>
<div v-else>
<div class="inputbtn">
<nya-input v-model.trim="current" fullwidth type="number" autofocus :label="`输入时间戳(${useSec ? '秒' : '毫秒'})`" :placeholder="`请输入时间戳(${useSec ? '秒' : '毫秒'})`" autocomplete="off" />
<button type="button" class="nya-btn" @click="setDate">
当前时间
</button>
</div>
<nya-checkbox v-model="useSec" class="mt-15" label="使用秒(s)为单位" />
</div>
<nya-checkbox v-model="chooseManually" class="mt-15" label="手动选择时间" />
</nya-container>
<nya-container v-show="results" title="结果">
<pre>{{ results }}</pre>
</nya-container>
</div>
</template>
<script>
let DatePicker;
if (process.browser) {
DatePicker = require('vue2-datepicker').default;
}
import dayjs from 'dayjs';
export default {
name: 'Timestamp',
head() {
return this.$store.state.currentTool.head;
},
components: {
DatePicker
},
data() {
return {
current: null,
useSec: false,
defaultValue: dayjs().format('YYYY-MM-DD'),
date: null,
chooseManually: false
};
},
computed: {
results() {
if (!this.current) return false;
const time = this.chooseManually
? this.date
: parseInt(this.current);
if (this.chooseManually) {
return this.date;
} else {
if (this.useSec) {
return dayjs.unix(time).format('YYYY-MM-DD HH:mm:ss');
} else {
return dayjs(time).format('YYYY-MM-DD HH:mm:ss');
}
}
}
},
methods: {
setDate() {
if (this.useSec) {
this.current = dayjs().unix();
} else {
this.current = new Date().getTime();
}
}
}
};
</script>
<style lang="scss">
.timestamp {
.date-picker {
width: 100%;
}
.nya-input {
width: 100%;
box-sizing: border-box;
.mx-input {
border-radius: 0;
height: 100%;
box-sizing: border-box;
}
}
}
</style>