手把手教你常用的59个JS类方法( 五 )

/** * 获取当前月天数 * @param {String} year 年份 * @param {String} month 月份 */export const getMonthNum = (year, month) => {var d = new Date(year, month, 0)return d.getDate()}复制代码55.getYyMmDdHhMmSs获取当前时间 yyyy-mm-dd,hh:mm:ss
/** * 获取当前时间 yyyy-mm-dd,hh:mm:ss */export const getYyMmDdHhMmSs = () => {const date = new Date()const year = date.getFullYear()const month = date.getMonth() + 1const day = date.getDate()const hours = date.getHours()const minu = date.getMinutes()const second = date.getSeconds()const arr = [month, day, hours, minu, second]arr.forEach(item => {item < 10 ? '0' + item : item})return (year +'-' +arr[0] +'-' +arr[1] +' ' +arr[2] +':' +arr[3] +':' +arr[4])}复制代码56.timesToYyMmDd时间戳转化为年月日
/** * 时间戳转化为年月日 * @param times 时间戳 * @param ymd 格式类型(yyyy-mm-dd,yyyy/mm/dd) * @param hms 可选,格式类型(hh,hh:mm,hh:mm:ss) * @returns {年月日} */export const timesToYyMmDd = (times, ymd,hms) => {const oDate = new Date(times)const oYear = oDate.getFullYear()const oMonth = oDate.getMonth() + 1const oDay = oDate.getDate()const oHour = oDate.getHours()const oMin = oDate.getMinutes()const oSec = oDate.getSeconds()let oTime // 最后拼接时间// 年月日格式switch (ymd) {case 'yyyy-mm-dd':oTime = oYear + '-' + getzf(oMonth) + '-' + getzf(oDay)breakcase 'yyyy/mm/dd':oTime = oYear + '/' + getzf(oMonth) + '/' + getzf(oDay)break}// 时分秒格式switch (hms) {case 'hh':oTime = ' '+oTime + getzf(oHour)breakcase 'hh:mm':oTime = oTime + getzf(oHour) + ':' + getzf(oMin)breakcase 'hh:mm:ss':oTime = oTime + getzf(oHour) + ':' + getzf(oMin) + ':' + getzf(oSec)break}return oTime}复制代码57.YyMmDdToTimes将年月日转化成时间戳
/** * 将年月日转化成时间戳 * @param {String} time yyyy/mm/dd 或yyyy-mm-dd 或yyyy-mm-dd hh:mm 或yyyy-mm-dd hh:mm:ss */export const YyMmDdToTimes = (time) => {return new Date(time.replace(/-/g, '/')).getTime()}复制代码58.compareTimeOneLessTwo/** *比较时间 1 小于时间 2 * @param {String} timeOne时间 1 * @param {String} timeTwo时间 2 */export const compareTimeOneLessTwo = (timeOne, timeTwo) => {// 判断 timeOne 和 timeTwo 是否return new Date(timeOne.replace(/-/g, '/')).getTime()<new Date(timeTwo.replace(/-/g, '/')).getTime()}复制代码url59.getQueryString获取 url 后面通过?传参的参数~~~~
/** *获取 url 后面通过?传参的参数 * @param {String} name */export function getQueryString(name) {const reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')const url = window.location.hrefconst search = url.substring(url.lastIndexOf('?') + 1)const r = search.match(reg)if (r != null) return unescape(r[2])return null}复制代码总结码字不易,持续更新中,欢迎 start!
github地址:https://github.com/lanzhsh/react-vue-koa/tree/master/utils-lan
原链接:https://juejin.im/post/5de5be53f265da05c33fcbb4




推荐阅读