时间戳转换为日期格式(天,小时,分,秒)
生活随笔
收集整理的這篇文章主要介紹了
时间戳转换为日期格式(天,小时,分,秒)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
相關(guān)Function
computeTime(timestamp: number) {const dateTypeArr = ['days ago', 'hours ago', 'minutes ago'];const now = Date.now();const duration = (now - timestamp) / 1000; // 計(jì)算當(dāng)前時(shí)間戳與傳入時(shí)間戳的差值const days = Math.floor(duration / 60 / 60 / 24);const hours = Math.floor((duration / 60 / 60) % 24);const mins = Math.floor((duration / 60) % 60);const seconds = Math.floor(duration % 60);const timeArr = [days, hours, mins, seconds];const index = timeArr.findIndex((item) => item > 0);if (index === timeArr.length - 1 || index === -1)return 'In few seconds ago'; return `${timeArr[index]} ${dateTypeArr[index]}`;} // 計(jì)算時(shí)間并返回booleancomputeTime(timestamp: number) {const now = Date.now();const duration = (now - timestamp) / 1000;const oneHour = 60 * 60; //計(jì)算時(shí)間差是否在一小時(shí)之內(nèi)if (duration >= oneHour) {return false;}return true;}總結(jié)
以上是生活随笔為你收集整理的时间戳转换为日期格式(天,小时,分,秒)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【日语口语词典学习】第0005页
- 下一篇: 针对零基础的UE开发(05)