JS17 -- 时间功能、时间与时间戳
一、获取时间戳
new Date().getTime() //ES5获取当前时间戳,13位毫秒
new Date().valueOf()
Date.now() // ES6获取当前时间戳
二、功能
①、时间戳转日期格式
/* *1. 时间戳转日期格式 *如:10或13位 => '2020-2-21 10:20:30' */ toStrTime(ntimestamp = new Date()) { const t = new Date(toString(ntimestamp) > 10 ? ntimestamp : ntimestamp * 1000) const crowdTime = `${t.getFullYear()}-${((t.getMonth() + 1) < 10) ? '0' + (t.getMonth() + 1) : t.getMonth() + 1}-${t.getDate() < 10 ? '0' + t.getDate() : t.getDate()} ${t.getHours() < 10 ? '0' + t.getHours() : t.getHours()}:${t.getMinutes() < 10 ? '0' + t.getMinutes() : t.getMinutes()}:${t.getSeconds() < 10 ? '0' + t.getSeconds() : t.getSeconds()}` return crowdTime }
②、计算两个时间对象的时间差,返回格式'01:12:10'
(原理,时间戳/(天/时/分/秒)=值, 时间戳 - (天/时/分/秒)*值 接着循环)
/* * 计算两个时间对象的时间差,返回格式'01:12:10' / '1天01:12:10' (如何超过一天,显示后者) * toTime(start,end), 格式'2020-07-31 13:38:22' | '2020/07/31 13:38:22' | 13位时间戳 | 10时间戳 四种其中一种,且start和end格式保存一致 * 如:timeTnterval('2020-8-12 12:10:30', '2020-8-13 12:50:29') => "1天 00:39:59" */ function timeTnterval(start, end){ if (start && end) { let tInterval; if(typeof start !== 'string') { if(start> 1000000000000 && end >1000000000000) { tInterval = end - start } else { tInterval = (end - start) * 1000 } } else { tInterval = new Date(end.replace(/-/g, '/')).getTime() - new Date(start.replace(/-/g, '/')).getTime() } const timeArr = [86400000, 3600000, 60000, 1000] // 一天、一小时、一分钟、一秒的毫秒数 let sum = '' for (const item of timeArr) { one:if (tInterval > item) { const intBt = parseInt(tInterval / item) if(item === timeArr[0]){ sum += parseInt(tInterval / item) + '天 ' } else { sum += (intBt > 9 ? intBt : `0${intBt}`) + (item === 1000 ? '' : ':') } tInterval = tInterval - item * intBt } else { if(item === timeArr[0]) { break one } sum += '00:' } } return sum } } let kk = timeTnterval('2020-8-12 12:10:30', '2020-8-13 12:50:29') let gg = timeTnterval(1597205430000, 1597294229000) let hh = timeTnterval(1597205430, 1597294229) console.log(kk) // "1天 00:39:59" console.log(gg) // "1天 00:39:59" console.log(hh) // "1天 00:39:59"
③、秒数 转 天时分秒,返回格式'01:12:10'
/* * 3. 秒数 转 天时分秒,用于计时和倒计时 * intervalTime(秒) => 1天19:20:12/19:20:12(不足一天返后者) */ export function intervalTime(count = 0) { let [day, hours, minus, second, sDay] = [0, 0, 0, 0, ''] second = showNum(count % 60) minus = showNum(parseInt(count / 60) % 60) day = parseInt(count / 60 / 60 / 24) if (day) { // 超过一天 hours = showNum(parseInt((count - day * 86400) / 60 / 60)) sDay = day + '天' } else { // 一天内 hours = showNum(parseInt(count / 60 / 60)) } return `${sDay}${hours}:${minus}:${second}` function showNum(num) { if (num < 10) { return '0' + num } return num } }
PHP 10位时间戳和13位时间戳
1 $nTime = time();
2 echo '获取当前时间戳10位='.$nTime;
3 echo '-----';
4 function timeEvery($str) { // 格式'Y/m/d H:i:s'
5 echo 'strtotime获取某时时间戳10位='.strtotime(date($str));
6 }
7 timeEvery("2020/1/21 19:30:10");
8 echo '-----';
9 function microtimeFn($num) { // 获取10-14位的时间戳,输入想要的长度,格式数字
10 $trueNum = $num - 10;
11 switch($trueNum) {
12 case 1:
13 $trueNum = 10;
14 break;
15 case 2:
16 $trueNum = 100;
17 break;
18 case 3:
19 $trueNum = 1000;
20 break;
21 case 4:
22 $trueNum = 10000;
23 break;
24 default:
25 $trueNum = 1;
26 break;
27
28 }
29
30 $microTime = (int)(microtime(true)*$trueNum);
31 echo '获取当前0-14位时间戳,当前为='.strLen($microTime).'位='.$microTime;
32 // echo $trueNum;
33 }
34 microtimeFn(14);
常见问题:
时间戳是13位的整数(1970年1月1号毫秒开始算),后端一般返回10位的秒数(if php)
'2020-07-31 13:38:22' | '2020/07/31 13:38:22' 前者有兼容性问题,解决 string.replace(/-/g, '/') ...
getMonth() getDay() 月份和周是0开始的,所以要加一
.lgyong