diff --git a/examples/app.vue b/examples/app.vue
index e8cc10b3..3b7d7611 100644
--- a/examples/app.vue
+++ b/examples/app.vue
@@ -62,6 +62,7 @@ nav {
ColorPicker
AutoComplete
Scroll
+ Time
diff --git a/examples/main.js b/examples/main.js
index 9d7c0e26..69dffda2 100644
--- a/examples/main.js
+++ b/examples/main.js
@@ -202,6 +202,10 @@ const router = new VueRouter({
{
path: '/scroll',
component: (resolve) => require(['./routers/scroll.vue'], resolve)
+ },
+ {
+ path: '/time',
+ component: (resolve) => require(['./routers/time.vue'], resolve)
}
]
});
diff --git a/examples/routers/time.vue b/examples/routers/time.vue
new file mode 100644
index 00000000..53471c8b
--- /dev/null
+++ b/examples/routers/time.vue
@@ -0,0 +1,18 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/components/time/index.js b/src/components/time/index.js
new file mode 100644
index 00000000..90c60880
--- /dev/null
+++ b/src/components/time/index.js
@@ -0,0 +1,2 @@
+import Time from './time.vue';
+export default Time;
\ No newline at end of file
diff --git a/src/components/time/time.js b/src/components/time/time.js
new file mode 100644
index 00000000..4a555bf8
--- /dev/null
+++ b/src/components/time/time.js
@@ -0,0 +1,83 @@
+/**
+ * @param {Number} timeStamp 判断时间戳格式是否是毫秒
+ * @returns {Boolean}
+ */
+// const isMillisecond = timeStamp => {
+// const timeStr = String(timeStamp)
+// return timeStr.length > 10
+// }
+
+/**
+ * @param {Number} timeStamp 传入的时间戳
+ * @param {Number} currentTime 当前时间时间戳
+ * @returns {Boolean} 传入的时间戳是否早于当前时间戳
+ */
+const isEarly = (timeStamp, currentTime) => {
+ return timeStamp < currentTime;
+};
+
+/**
+ * @param {Number} num 数值
+ * @returns {String} 处理后的字符串
+ * @description 如果传入的数值小于10,即位数只有1位,则在前面补充0
+ */
+const getHandledValue = num => {
+ return num < 10 ? '0' + num : num;
+};
+
+/**
+ * @param {Number} timeStamp 传入的时间戳
+ * @param {Number} startType 要返回的时间字符串的格式类型,传入'year'则返回年开头的完整时间
+ */
+const getDate = (timeStamp, startType) => {
+ const d = new Date(timeStamp * 1000);
+ const year = d.getFullYear();
+ const month = getHandledValue(d.getMonth() + 1);
+ const date = getHandledValue(d.getDate());
+ const hours = getHandledValue(d.getHours());
+ const minutes = getHandledValue(d.getMinutes());
+ const second = getHandledValue(d.getSeconds());
+ let resStr = '';
+ if (startType === 'year') resStr = year + '-' + month + '-' + date + ' ' + hours + ':' + minutes + ':' + second;
+ else resStr = month + '-' + date + ' ' + hours + ':' + minutes;
+ return resStr;
+};
+
+/**
+ * @param {String|Number} timeStamp 时间戳
+ * @returns {String} 相对时间字符串
+ */
+export const getRelativeTime = timeStamp => {
+ // 判断当前传入的时间戳是秒格式还是毫秒
+ const IS_MILLISECOND = true;
+ // 如果是毫秒格式则转为秒格式
+ if (IS_MILLISECOND) Math.floor(timeStamp /= 1000);
+ // 传入的时间戳可以是数值或字符串类型,这里统一转为数值类型
+ timeStamp = Number(timeStamp);
+ // 获取当前时间时间戳
+ const currentTime = Math.floor(Date.parse(new Date()) / 1000);
+ // 判断传入时间戳是否早于当前时间戳
+ const IS_EARLY = isEarly(timeStamp, currentTime);
+ // 获取两个时间戳差值
+ let diff = currentTime - timeStamp;
+ // 如果IS_EARLY为false则差值取反
+ if (!IS_EARLY) diff = -diff;
+ let resStr = '';
+ const dirStr = IS_EARLY ? '前' : '后';
+ // 少于等于59秒
+ if (diff <= 59) resStr = diff + '秒' + dirStr;
+ // 多于59秒,少于等于59分钟59秒
+ else if (diff > 59 && diff <= 3599) resStr = Math.floor(diff / 60) + '分钟' + dirStr;
+ // 多于59分钟59秒,少于等于23小时59分钟59秒
+ else if (diff > 3599 && diff <= 86399) resStr = Math.floor(diff / 3600) + '小时' + dirStr;
+ // 多于23小时59分钟59秒,少于等于29天59分钟59秒
+ else if (diff > 86399 && diff <= 2623859) resStr = Math.floor(diff / 86400) + '天' + dirStr;
+ // 多于29天59分钟59秒,少于364天23小时59分钟59秒,且传入的时间戳早于当前
+ else if (diff > 2623859 && diff <= 31567859 && IS_EARLY) resStr = getDate(timeStamp);
+ else resStr = getDate(timeStamp, 'year');
+ return resStr;
+};
+
+export default function (timestamp) {
+ return getRelativeTime(timestamp);
+}
\ No newline at end of file
diff --git a/src/components/time/time.vue b/src/components/time/time.vue
new file mode 100644
index 00000000..7ef737ad
--- /dev/null
+++ b/src/components/time/time.vue
@@ -0,0 +1,95 @@
+
+ {{ date }}
+
+
\ No newline at end of file
diff --git a/src/index.js b/src/index.js
index ee7d67dc..a2a83b6d 100644
--- a/src/index.js
+++ b/src/index.js
@@ -42,6 +42,7 @@ import Switch from './components/switch';
import Table from './components/table';
import Tabs from './components/tabs';
import Tag from './components/tag';
+import Time from './components/time';
import Timeline from './components/timeline';
import TimePicker from './components/time-picker';
import Tooltip from './components/tooltip';
@@ -114,6 +115,7 @@ const components = {
Tabs: Tabs,
TabPane: Tabs.Pane,
Tag,
+ Time,
Timeline,
TimelineItem: Timeline.Item,
TimePicker,
@@ -138,7 +140,8 @@ const iview = {
iProgress: Progress,
iSelect: Select,
iSwitch: Switch,
- iTable: Table
+ iTable: Table,
+ iTime: Time
};
const install = function(Vue, opts = {}) {
diff --git a/src/styles/components/index.less b/src/styles/components/index.less
index cb877ad4..3fae1162 100644
--- a/src/styles/components/index.less
+++ b/src/styles/components/index.less
@@ -44,3 +44,4 @@
@import "avatar";
@import "color-picker";
@import "auto-complete";
+@import "time";
\ No newline at end of file
diff --git a/src/styles/components/time.less b/src/styles/components/time.less
new file mode 100644
index 00000000..70ce0639
--- /dev/null
+++ b/src/styles/components/time.less
@@ -0,0 +1,10 @@
+@time-prefix-cls: ~"@{css-prefix}time";
+
+.@{time-prefix-cls} {
+ &-with-hash{
+ cursor: pointer;
+ &:hover{
+ text-decoration: underline;
+ }
+ }
+}
\ No newline at end of file