update Time

This commit is contained in:
梁灏 2018-07-09 17:00:48 +08:00
parent b23916296f
commit d204147113
2 changed files with 15 additions and 12 deletions

View file

@ -4,16 +4,16 @@
<br> <br>
<Time :time="time2" /> <Time :time="time2" />
<br> <br>
<Time :time="time3" :interval="60" /> <Time :time="time3" :interval="1" />
</div> </div>
</template> </template>
<script> <script>
export default { export default {
data () { data () {
return { return {
time1: (new Date()).getTime() - 60 * 1 * 1000, time1: (new Date()).getTime() - 60 * 59 * 1000,
time2: (new Date()).getTime() - 86400 * 3 * 1000, time2: (new Date()).getTime() - 86400 * 3 * 1000,
time3: new Date() time3: (new Date()).getTime() - 1 * 1000
} }
} }
} }

View file

@ -51,11 +51,13 @@ export const getRelativeTime = timeStamp => {
// 判断当前传入的时间戳是秒格式还是毫秒 // 判断当前传入的时间戳是秒格式还是毫秒
const IS_MILLISECOND = true; const IS_MILLISECOND = true;
// 如果是毫秒格式则转为秒格式 // 如果是毫秒格式则转为秒格式
if (IS_MILLISECOND) Math.floor(timeStamp /= 1000); // if (IS_MILLISECOND) Math.floor(timeStamp /= 1000);
// 传入的时间戳可以是数值或字符串类型,这里统一转为数值类型 // 传入的时间戳可以是数值或字符串类型,这里统一转为数值类型
timeStamp = Number(timeStamp); // timeStamp = Number(timeStamp);
// 获取当前时间时间戳 // 获取当前时间时间戳
const currentTime = Math.floor(Date.parse(new Date()) / 1000); // const currentTime = Math.floor(Date.parse(new Date()) / 1000);
const currentTime = (new Date()).getTime();
// 判断传入时间戳是否早于当前时间戳 // 判断传入时间戳是否早于当前时间戳
const IS_EARLY = isEarly(timeStamp, currentTime); const IS_EARLY = isEarly(timeStamp, currentTime);
// 获取两个时间戳差值 // 获取两个时间戳差值
@ -64,17 +66,18 @@ export const getRelativeTime = timeStamp => {
if (!IS_EARLY) diff = -diff; if (!IS_EARLY) diff = -diff;
let resStr = ''; let resStr = '';
const dirStr = IS_EARLY ? '前' : '后'; const dirStr = IS_EARLY ? '前' : '后';
if (diff < 1000) resStr = '刚刚';
// 少于等于59秒 // 少于等于59秒
if (diff < 1) resStr = '刚刚'; else if (diff < 60000) resStr = parseInt(diff / 1000) + '秒' + dirStr;
else if (diff <= 59) resStr = parseInt(diff) + '秒' + dirStr;
// 多于59秒少于等于59分钟59秒 // 多于59秒少于等于59分钟59秒
else if (diff > 59 && diff <= 3599) resStr = Math.ceil(diff / 60) + '分钟' + dirStr; else if (diff >= 60000 && diff < 3600000) resStr = Math.floor(diff / 60000) + '分钟' + dirStr;
// 多于59分钟59秒少于等于23小时59分钟59秒 // 多于59分钟59秒少于等于23小时59分钟59秒
else if (diff > 3599 && diff <= 86399) resStr = Math.ceil(diff / 3600) + '小时' + dirStr; else if (diff >= 3600000 && diff < 86400000) resStr = Math.floor(diff / 3600000) + '小时' + dirStr;
// 多于23小时59分钟59秒少于等于29天59分钟59秒 // 多于23小时59分钟59秒少于等于29天59分钟59秒
else if (diff > 86399 && diff <= 2623859) resStr = Math.ceil(diff / 86400) + '天' + dirStr; else if (diff >= 86400000 && diff < 2623860000) resStr = Math.floor(diff / 86400000) + '天' + dirStr;
// 多于29天59分钟59秒少于364天23小时59分钟59秒且传入的时间戳早于当前 // 多于29天59分钟59秒少于364天23小时59分钟59秒且传入的时间戳早于当前
else if (diff > 2623859 && diff <= 31567859 && IS_EARLY) resStr = getDate(timeStamp); else if (diff >= 2623860000 && diff <= 31567860000 && IS_EARLY) resStr = getDate(timeStamp);
else resStr = getDate(timeStamp, 'year'); else resStr = getDate(timeStamp, 'year');
return resStr; return resStr;
}; };