update Time func
This commit is contained in:
parent
468bde3c7d
commit
de30f1b30d
2 changed files with 79 additions and 91 deletions
|
@ -2,14 +2,14 @@
|
||||||
<div>
|
<div>
|
||||||
<Time :time="1526608921" />
|
<Time :time="1526608921" />
|
||||||
<Time :time="1652839997" />
|
<Time :time="1652839997" />
|
||||||
<Time :time="ddd" />
|
<Time :time="ddd" interval="1" />
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
<script>
|
<script>
|
||||||
export default {
|
export default {
|
||||||
data () {
|
data () {
|
||||||
return {
|
return {
|
||||||
ddd: new Date('2018-01-01')
|
ddd: new Date('2019-05-28 14:12:00')
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
computed: {},
|
computed: {},
|
||||||
|
|
|
@ -1,97 +1,85 @@
|
||||||
const time = {
|
/**
|
||||||
getUnix (place) {
|
* @param {Number} timeStamp 判断时间戳格式是否是毫秒
|
||||||
const date = new Date();
|
* @returns {Boolean}
|
||||||
const timestamp = date.getTime(); // 得到的是毫秒
|
*/
|
||||||
|
// const isMillisecond = timeStamp => {
|
||||||
|
// const timeStr = String(timeStamp);
|
||||||
|
// return timeStr.length > 10;
|
||||||
|
// };
|
||||||
|
|
||||||
if (place === 's') { // 秒
|
/**
|
||||||
return Math.floor(timestamp / 1000);
|
* @param {Number} timeStamp 传入的时间戳
|
||||||
} else { // 毫秒
|
* @param {Number} currentTime 当前时间时间戳
|
||||||
return timestamp;
|
* @returns {Boolean} 传入的时间戳是否早于当前时间戳
|
||||||
}
|
*/
|
||||||
},
|
const isEarly = (timeStamp, currentTime) => {
|
||||||
getToday (place) {
|
return timeStamp < currentTime;
|
||||||
const today = new Date();
|
};
|
||||||
const year = today.getFullYear();
|
|
||||||
const month = today.getMonth() + 1;
|
|
||||||
const day = today.getDate();
|
|
||||||
const hours = 0;
|
|
||||||
const mins = 0;
|
|
||||||
const secs = 0;
|
|
||||||
const datetime = year + '-' + month + '-' + day + ' ' + hours + ':' + mins + ':' + secs;
|
|
||||||
let tmp_datetime = datetime.replace(/:/g,'-');
|
|
||||||
tmp_datetime = tmp_datetime.replace(/ /g,'-');
|
|
||||||
const arr = tmp_datetime.split('-');
|
|
||||||
const now = new Date(Date.UTC(arr[0],arr[1]-1,arr[2],arr[3]-8,arr[4],arr[5]));
|
|
||||||
|
|
||||||
if (place === 's') {
|
/**
|
||||||
return parseInt(now.getTime() / 1000);
|
* @param {Number} num 数值
|
||||||
} else {
|
* @returns {String} 处理后的字符串
|
||||||
return parseInt(now.getTime());
|
* @description 如果传入的数值小于10,即位数只有1位,则在前面补充0
|
||||||
}
|
*/
|
||||||
},
|
const getHandledValue = num => {
|
||||||
getYear (place) {
|
return num < 10 ? '0' + num : num;
|
||||||
const today = new Date();
|
};
|
||||||
const year = today.getFullYear();
|
|
||||||
const month = 0;
|
|
||||||
const day = 1;
|
|
||||||
const hours = 0;
|
|
||||||
const mins = 0;
|
|
||||||
const secs = 0;
|
|
||||||
const datetime = year + '-' + month + '-' + day + ' ' + hours + ':' + mins + ':' + secs;
|
|
||||||
let tmp_datetime = datetime.replace(/:/g,'-');
|
|
||||||
tmp_datetime = tmp_datetime.replace(/ /g,'-');
|
|
||||||
const arr = tmp_datetime.split('-');
|
|
||||||
const now = new Date(Date.UTC(arr[0],arr[1]-1,arr[2],arr[3]-8,arr[4],arr[5]));
|
|
||||||
if (place === 's') {
|
|
||||||
return parseInt(now.getTime() / 1000);
|
|
||||||
} else {
|
|
||||||
return parseInt(now.getTime());
|
|
||||||
}
|
|
||||||
},
|
|
||||||
getLastDate (time, type) {
|
|
||||||
const unixtime = time * 1000;
|
|
||||||
const date = new Date(unixtime);
|
|
||||||
const month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1;
|
|
||||||
const currentDate = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
|
|
||||||
const hh = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
|
|
||||||
const mm = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
|
|
||||||
// const ss = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
|
|
||||||
|
|
||||||
if (type === 'month') {
|
/**
|
||||||
return month + '-' + currentDate + ' ' + hh + ':' + mm;
|
* @param {Number} timeStamp 传入的时间戳
|
||||||
//return month + "-" + currentDate;
|
* @param {Number} startType 要返回的时间字符串的格式类型,传入'year'则返回年开头的完整时间
|
||||||
} else {
|
*/
|
||||||
return date.getFullYear() + '-' + month + '-' + currentDate + ' ' + hh + ':' + mm;
|
const getDate = (timeStamp, startType) => {
|
||||||
}
|
const d = new Date(timeStamp * 1000);
|
||||||
},
|
const year = d.getFullYear();
|
||||||
// 时间戳转详细时间,比如5分钟前
|
const month = getHandledValue(d.getMonth() + 1);
|
||||||
getMagic (timestamp) {
|
const date = getHandledValue(d.getDate());
|
||||||
const now = this.getUnix('s'); //当前时间戳
|
const hours = getHandledValue(d.getHours());
|
||||||
const today = this.getToday('s'); //今天0点时间戳
|
const minutes = getHandledValue(d.getMinutes());
|
||||||
const year = this.getYear('s'); //今年0点时间戳
|
const second = getHandledValue(d.getSeconds());
|
||||||
const timer = now - timestamp;
|
let resStr = '';
|
||||||
|
if (startType === 'year') resStr = year + '-' + month + '-' + date + ' ' + hours + ':' + minutes + ':' + second;
|
||||||
|
else resStr = month + '-' + date + ' ' + hours + ':' + minutes;
|
||||||
|
return resStr;
|
||||||
|
};
|
||||||
|
|
||||||
let tip = '';
|
/**
|
||||||
|
* @param {String|Number} timeStamp 时间戳
|
||||||
if (timer <= 0) {
|
* @returns {String} 相对时间字符串
|
||||||
tip = '刚刚';
|
*/
|
||||||
} else if (Math.floor(timer/60) <= 0) {
|
export const getRelativeTime = timeStamp => {
|
||||||
tip = '刚刚';
|
// 判断当前传入的时间戳是秒格式还是毫秒
|
||||||
} else if (timer < 3600) {
|
// const IS_MILLISECOND = isMillisecond(timeStamp);
|
||||||
tip = Math.floor(timer/60) + '分钟前';
|
const IS_MILLISECOND = true;
|
||||||
} else if (timer >= 3600 && (timestamp - today >= 0) ) {
|
// 如果是毫秒格式则转为秒格式
|
||||||
tip = Math.floor(timer/3600) + '小时前';
|
if (IS_MILLISECOND) Math.floor(timeStamp /= 1000);
|
||||||
} else if (timer/86400 <= 31) {
|
// 传入的时间戳可以是数值或字符串类型,这里统一转为数值类型
|
||||||
tip = Math.ceil(timer/86400) + '天前';
|
timeStamp = Number(timeStamp);
|
||||||
} else if (timestamp - today < 0 && (timestamp - year >= 0)) {
|
// 获取当前时间时间戳
|
||||||
tip = this.getLastDate(timestamp, 'month');
|
const currentTime = Math.floor(Date.parse(new Date()) / 1000);
|
||||||
} else {
|
// 判断传入时间戳是否早于当前时间戳
|
||||||
tip = this.getLastDate(timestamp, 'year');
|
const IS_EARLY = isEarly(timeStamp, currentTime);
|
||||||
}
|
// 获取两个时间戳差值
|
||||||
return tip;
|
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) resStr = getDate(timeStamp);
|
||||||
|
// 多于364天23小时59分钟59秒
|
||||||
|
else resStr = getDate(timeStamp, 'year');
|
||||||
|
return resStr;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default function (timestamp) {
|
export default function (timestamp) {
|
||||||
return time.getMagic(timestamp / 1000);
|
return getRelativeTime(timestamp);
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue