获取某个月的第一天和最后一天
/*** @param {Date} cur,时间对象数据*/function getFirstDay(cur){let firstDay = new Date();if(cur) firstDay = cur;firstDay = new Date(firstDay.getFullYear() + '/' + (firstDay.getMonth()+1) + '/1');return firstDay;}// 核心是setDate方法,0为上个月最后一天,(1~31)为本月某一天function getLastDay(cur){let lastDay = new Date();if(cur) lastDay = cur;lastDay.setMonth(lastDay.getMonth() + 1);lastDay.setDate(0);return lastDay;}// 使用getLastDay(new Date("2020/2/5")) // Sat Feb 29 2020 00:00:00 GMT+0800 (中国标准时间)
生成一周时间
function getWeekTime(){return [...new Array(7)].map((item,i) => new Date(Date.now()+i*8.64e7).toLocaleDateString())}
类型判断
判断核心使用Object.prototype.toString.call
function checkType(target, type){let targetType = Object.prototype.toString.call(target).slice(8,-1).toLowerCase();return targetType === type.toLowerCase();}// 使用checkType([], 'Array') // truecheckType(/\d/, 'RegExp') // truecheckType(new Date(), 'Date') // truecheckType(function(){}, 'Function') // truecheckType(Symbol(1), 'Symbol') // true
白屏时间
Web Performance API允许网页访问某些函数来测量网页和Web应用程序的性能performance.timing 包含延迟相关的性能信息performance.memory 包含内存信息,是Chrome中添加的一个非标准扩展,在使用时需要注意
window.onload = function(){setTimeout(()=>{let t = performance.timing,m = performance.memoryconsole.table({'DNS查询耗时': (t.domainLookupEnd - t.domainLookupStart).toFixed(0),'TCP链接耗时': (t.connectEnd - t.connectStart).toFixed(0),'request请求耗时': (t.responseEnd - t.responseStart).toFixed(0),'解析dom树耗时': (t.domComplete - t.domInteractive).toFixed(0),'白屏时间': (t.responseStart - t.navigationStart).toFixed(0),'domready时间': (t.domContentLoadedEventEnd - t.navigationStart).toFixed(0),'onload时间': (t.loadEventEnd - t.navigationStart).toFixed(0),'js内存使用占比': m ? (m.usedJSHeapSize / m.totalJSHeapSize * 100).toFixed(2) + '%' : undefined})})}
