update Time

This commit is contained in:
梁灏 2018-05-18 10:32:51 +08:00
parent 42f644a31c
commit eb37e68cd4
3 changed files with 132 additions and 4 deletions

View file

@ -5,6 +5,7 @@
import Vue from 'vue';
const isServer = Vue.prototype.$isServer;
import { oneOf } from '../../utils/assist';
import Time from './time';
const prefixCls = 'ivu-time';
@ -12,7 +13,7 @@
name: 'Time',
props: {
time: {
type: [String, Number, Date],
type: [Number, Date],
required: true
},
type: {
@ -51,7 +52,33 @@
if (this.hash !== '') window.location.hash = this.hash;
},
setTime () {
this.date = this.time;
const type = typeof this.time;
let time;
if (type === 'number') {
const timestamp = this.time.toString().length > 10 ? this.time : this.time * 1000;
time = (new Date(timestamp)).getTime();
} else if (type === 'object') {
time = this.time.getTime();
}
if (this.type === 'relative') {
this.date = Time(time);
} else {
const date = new Date(this.time);
const year = date.getFullYear();
const month = (date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1);
const day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
const hour = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
const minute = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
const second = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
if (this.type === 'datetime') {
this.date = `${year}-${month}-${day} ${hour}:${minute}:${second}`;
} else if (this.type === 'date') {
this.date = `${year}-${month}-${day}`;
}
}
}
},
mounted () {