updateAt:Fri Oct 04 2019 17:34:22 GMT+0800 (中國標準時間)html
const UA = navigator.userAgent; // 判斷終端類型 const isWechatBrowser = () => { return /micromessenger/i.test(UA); }; const isMobile = () => { const regExp = /ipad|iphone|android|blackberry|windows phone|webos/i return regExp.test(UA) } const isIos = () => { return /iphone|ipad|ipod/i.test(UA) } const isiPhone = () => { return /iphone/i.test(UA) } const isIpad = () => { return /ipad/i.test(UA) } const isIpod = () => { return /ipod/i.test(UA) } const isAndroid = () => { return /android/i.test(UA) } /*判斷瀏覽器類型*/ const isFirefox = () => { return /firefox/i.test(UA); } const isChrome = () => { return /chrome/i.test(UA) } exports {isWechatBrowser}
const url = location.href; export const urlQueryToObject = (url) => { if((/\?/).test(url)) { const arr = url.split('?')[1].split('&'); return arr.reduce((acc,value,index) => {value.replace(/(\w+)=(\w+)/,(match,p1,p2) => {acc[p1] = p2});return acc},{}); } return {} } // exapmple // location.href="http:example.com/index.html?a=1&b=2" urlQueryToObject(location.href) // {a:1,b:2} export const urlQueryToArr = url => { if((/\?/).test(url)) { const arr = url.split('?')[1].split('&'); return arr.reduce((acc,value,index) => {acc.push(value.split('='));return acc;},[]) } return [] }
export const createAt = date => { var time = new Date(date); var year = time.getFullYear(); var month = time.getMonth() + 1; var day = time.getDate(); var hh = time.getHours(); var mm = time.getMinutes(); return year + '年' + month + '月' + day + '日' + ' ' + hh + ':' + mm; } export const timeAgo = (date) => { var seconds = Math.floor((new Date() - new Date(date)) / 1000); const yearseconds = 365 * 24 * 60 * 60;// 一年有多少秒 const monthseconds = 30 * 24 * 60 * 60; // 一個月有多少秒 const dateseconds = 24 * 60 * 60;// 一天有多少秒 let interval = 0; if (seconds > yearseconds) { return createAt(date); } else if (seconds > monthseconds) { interval = Math.floor(seconds / monthseconds); return interval + '月前'; } else if (seconds > dateseconds) { interval = Math.floor(seconds / dateseconds); return interval + '天前'; } else if (seconds > 3600) { interval = Math.floor(seconds / 3600); return interval + '小時前'; } else if (seconds > 60) { interval = Math.floor(seconds / 60); return interval + '分鐘前'; } else { return '剛剛'; } },
參考:android