123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- const formatTime = date => {
- const year = date.getFullYear()
- const month = date.getMonth() + 1
- const day = date.getDate()
- const hour = date.getHours()
- const minute = date.getMinutes()
- const second = date.getSeconds()
- return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
- }
- const formatTime2 = date => {
- const year = date.getFullYear()
- const month = date.getMonth() + 1
- const day = date.getDate()
- const hour = date.getHours()
- const minute = date.getMinutes()
- const second = date.getSeconds()
- return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
- }
- const formatNumber = n => {
- n = n.toString()
- return n[1] ? n : '0' + n
- }
- const formatDate = date => {
- const year = date.getFullYear()
- const month = date.getMonth() + 1
- const day = date.getDate()
- const hour = date.getHours()
- const minute = date.getMinutes()
- const second = date.getSeconds()
- return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
- }
- const formatTimeTwo = date => {
- const hour = date.getHours()
- const minute = date.getMinutes()
- const second = date.getSeconds()
- return [hour, minute, second].map(formatNumber).join(':')
- }
- const formatUrl = (url, obj) => {
- let temp = []
- let fn = (obj) => {
- let temp = []
- for (let i in obj) {
- if (obj[i] != null && typeof obj[i] == 'object') {
- temp.push(`${i}=${JSON.stringify(obj[i])}`)
- } else {
- temp.push(`${i}=${obj[i]}`)
- }
- }
- return temp.join('&')
- }
- return url.indexOf('?') < 0 ? url + '?' + fn(obj) : url + '&' + fn(obj)
- }
- function throttle(fn, gapTime) {
- if (gapTime == null || gapTime == undefined) {
- gapTime = 1500
- }
- let _lastTime = null
-
- return function () {
- let _nowTime = + new Date()
- if (_nowTime - _lastTime > gapTime || !_lastTime) {
- fn.apply(this, arguments)
- _lastTime = _nowTime
- }
- }
- }
- module.exports = {
- formatTime,
- formatTime2,
- formatUrl,
- formatDate,
- formatTimeTwo,
- formatNumber,
- throttle
- }
|