javaScript的Data函数
Posted 杨万生
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javaScript的Data函数相关的知识,希望对你有一定的参考价值。
1、获取当前时间
Date()获取到的时间是当前设备的显示的时间,开发中要考虑到用户的设备时间是否正确
let nowTime = new Date(); // 获取当前时间
把data时间转换成常规格式
<script> let getTimeNow = () => { let nowTime = new Date(); // 获取当前时间——Sun Jan 12 2020 14:07:22 GMT+0800 (中国标准时间) let year = nowTime.getFullYear(); // 年 let month = nowTime.getMonth()+1; // 月 这里要加1 let day = nowTime.getDate(); // 获取几号 let hour = nowTime.getHours();//得到小时 let minu = nowTime.getMinutes();//得到分钟 let sec = nowTime.getSeconds();//得到秒 let week = nowTime.getDay();//得到星期几 let timeStr = ‘‘; timeStr += year + ‘/‘ + isGreaterthanTen(month) + ‘/‘ + isGreaterthanTen(day); timeStr += ‘-‘ + isGreaterthanTen(hour) + ‘/‘ + isGreaterthanTen(minu) + ‘/‘ + isGreaterthanTen(sec); timeStr += ‘-‘ + getWeek(week); return timeStr; }; let isGreaterthanTen = num => { if (parseInt(num, 10) >= 10) { return num; } else { return ‘0‘ + num; } }; let getWeek = week => { if (parseInt(week, 10) === 0) { return ‘星期日‘ } else if (parseInt(week, 10) === 1) { return ‘星期一‘ } else if (parseInt(week, 10) === 2) { return ‘星期二‘ } else if (parseInt(week, 10) === 3) { return ‘星期三‘ } else if (parseInt(week, 10) === 4) { return ‘星期四‘ } else if (parseInt(week, 10) === 5) { return ‘星期五‘ } else { return ‘星期六‘ } }; console.log(getTimeNow()); // 2020/01/12-14/25/29-星期日 </script>
2、时间戳
获取当前时间戳:
// 获取当前时间戳 let timestamp1 = Date.parse(new Date()); // 精确到秒 let timestamp2 = (new Date()).valueOf(); // 精确到毫秒 let timestamp3 = new Date().getTime(); // 精确到毫秒 let timestamp4 = Number(new Date()) ; // 精确到毫秒 let timestamp5 = Date.now(); // 精确到毫秒
把时间转换成时间戳
<script> let getTimeNow = (nowTime) => { // let nowTime = new Date(); // 获取当前时间——Sun Jan 12 2020 14:07:22 GMT+0800 (中国标准时间) let year = nowTime.getFullYear(); // 年 let month = nowTime.getMonth()+1; // 月 这里要加1 let day = nowTime.getDate(); // 获取几号 let hour = nowTime.getHours();//得到小时 let minu = nowTime.getMinutes();//得到分钟 let sec = nowTime.getSeconds();//得到秒 let week = nowTime.getDay();//得到星期几 let timeStr = ‘‘; timeStr += year + ‘/‘ + isGreaterthanTen(month) + ‘/‘ + isGreaterthanTen(day); timeStr += ‘-‘ + isGreaterthanTen(hour) + ‘/‘ + isGreaterthanTen(minu) + ‘/‘ + isGreaterthanTen(sec); timeStr += ‘-‘ + getWeek(week); return timeStr; }; let isGreaterthanTen = num => { if (parseInt(num, 10) >= 10) { return num; } else { return ‘0‘ + num; } }; let getWeek = week => { if (parseInt(week, 10) === 0) { return ‘星期日‘ } else if (parseInt(week, 10) === 1) { return ‘星期一‘ } else if (parseInt(week, 10) === 2) { return ‘星期二‘ } else if (parseInt(week, 10) === 3) { return ‘星期三‘ } else if (parseInt(week, 10) === 4) { return ‘星期四‘ } else if (parseInt(week, 10) === 5) { return ‘星期五‘ } else { return ‘星期六‘ } }; // 获取当前时间戳 let timestamp1 = Date.parse(new Date()); // 精确到秒 let timestamp2 = (new Date()).valueOf(); // 精确到毫秒 let timestamp3 = new Date().getTime(); // 精确到毫秒 let timestamp4 = Number(new Date()) ; // 精确到毫秒 let timestamp5 = Date.now(); // 精确到毫秒 let timeStr = new Date(1578811134211); // 放入时间戳 console.log(getTimeNow(timeStr)); // 2020/01/12-14/38/54-星期日 console.log(timeStr.toLocaleDateString().replace(///g, "-") + " " + timeStr.toTimeString().substr(0, 8)); //再利用拼接正则等手段转化为yyyy-MM-dd hh:mm:ss 格式 </script>
3、倒计时
计算时间戳
<script> let timeEnd = 1578987896323; // 结束时间的时间戳 let countDown = (timeEnd) => { let timeNow = new Date().getTime(); // 当前时间戳 let remainingTime = parseInt(timeEnd) - parseInt(timeNow); secondTime(remainingTime); }; let isGreaterthanTen = num => { if (parseInt(num, 10) >= 10) { return num; } else { return ‘0‘ + num; } }; let secondTime = (remainingTime) => { /* 1 秒 s = 1000 毫秒 ms 1 分 min = 60 秒 s 1 时 hour = 60分 min 1 天 day = 24 小时 hour 1天等于 86400000毫秒(ms) * */ let timeStr = ‘‘; let days = Math.floor(remainingTime/(24*3600*1000)); // 相差天数 if (days > 0) { timeStr += isGreaterthanTen(days) + ‘天‘; } else { timeStr += ‘00天‘ } let hoursLeave = remainingTime%(24*3600*1000) //计算天数后剩余的毫秒数 let hours = Math.floor(hoursLeave/(3600*1000)); // 相差小时数 if (hours > 0) { timeStr += isGreaterthanTen(hours) + ‘时‘; } else { timeStr += ‘00时‘; } let minLeave = hoursLeave%(3600*1000); //计算小时数后剩余的毫秒数 let minutes = Math.floor(minLeave/(60*1000)); if (minutes > 0) { timeStr += isGreaterthanTen(minutes) + ‘分‘; } else { timeStr += ‘00分‘; } let secounLeave =minLeave%(60*1000); //计算分钟数后剩余的毫秒数 let seconds=Math.round(secounLeave/1000); if (seconds > 0) { timeStr += isGreaterthanTen(seconds) + ‘秒‘ } else { timeStr += ‘00秒‘; } return timeStr; } console.log(countDown(timeEnd)); </script>
4、yyyy-MM-dd HH:mm:ss 转换成时间戳
// 获取某个时间格式的时间戳 let stringTime = "2020-01-01 00:00:00"; let timestamp = (new Date(stringTime)).getTime(); console.log(timestamp);
以上是关于javaScript的Data函数的主要内容,如果未能解决你的问题,请参考以下文章