master
parent
661a9166a9
commit
f0fe67768c
@ -1,117 +1,177 @@
|
|||||||
/**
|
/**
|
||||||
* Created by PanJiaChen on 16/11/18.
|
* Created by PanJiaChen on 16/11/18.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse the time to string
|
* Parse the time to string
|
||||||
* @param {(Object|string|number)} time
|
* @param {(Object|string|number)} time
|
||||||
* @param {string} cFormat
|
* @param {string} cFormat
|
||||||
* @returns {string | null}
|
* @returns {string | null}
|
||||||
*/
|
*/
|
||||||
export function parseTime(time, cFormat) {
|
export function parseTime(time, cFormat) {
|
||||||
if (arguments.length === 0 || !time) {
|
if (arguments.length === 0 || !time) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
|
const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
|
||||||
let date
|
let date
|
||||||
if (typeof time === 'object') {
|
if (typeof time === 'object') {
|
||||||
date = time
|
date = time
|
||||||
} else {
|
} else {
|
||||||
if ((typeof time === 'string')) {
|
if ((typeof time === 'string')) {
|
||||||
if ((/^[0-9]+$/.test(time))) {
|
if ((/^[0-9]+$/.test(time))) {
|
||||||
// support "1548221490638"
|
// support "1548221490638"
|
||||||
time = parseInt(time)
|
time = parseInt(time)
|
||||||
} else {
|
} else {
|
||||||
// support safari
|
// support safari
|
||||||
// https://stackoverflow.com/questions/4310953/invalid-date-in-safari
|
// https://stackoverflow.com/questions/4310953/invalid-date-in-safari
|
||||||
time = time.replace(new RegExp(/-/gm), '/')
|
time = time.replace(new RegExp(/-/gm), '/')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((typeof time === 'number') && (time.toString().length === 10)) {
|
if ((typeof time === 'number') && (time.toString().length === 10)) {
|
||||||
time = time * 1000
|
time = time * 1000
|
||||||
}
|
}
|
||||||
date = new Date(time)
|
date = new Date(time)
|
||||||
}
|
}
|
||||||
const formatObj = {
|
const formatObj = {
|
||||||
y: date.getFullYear(),
|
y: date.getFullYear(),
|
||||||
m: date.getMonth() + 1,
|
m: date.getMonth() + 1,
|
||||||
d: date.getDate(),
|
d: date.getDate(),
|
||||||
h: date.getHours(),
|
h: date.getHours(),
|
||||||
i: date.getMinutes(),
|
i: date.getMinutes(),
|
||||||
s: date.getSeconds(),
|
s: date.getSeconds(),
|
||||||
a: date.getDay()
|
a: date.getDay()
|
||||||
}
|
}
|
||||||
const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
|
const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
|
||||||
const value = formatObj[key]
|
const value = formatObj[key]
|
||||||
// Note: getDay() returns 0 on Sunday
|
// Note: getDay() returns 0 on Sunday
|
||||||
if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value ] }
|
if (key === 'a') {
|
||||||
return value.toString().padStart(2, '0')
|
return ['日', '一', '二', '三', '四', '五', '六'][value]
|
||||||
})
|
}
|
||||||
return time_str
|
return value.toString().padStart(2, '0')
|
||||||
}
|
})
|
||||||
|
return time_str
|
||||||
/**
|
}
|
||||||
* @param {number} time
|
|
||||||
* @param {string} option
|
/**
|
||||||
* @returns {string}
|
* @param {number} time
|
||||||
*/
|
* @param {string} option
|
||||||
export function formatTime(time, option) {
|
* @returns {string}
|
||||||
if (('' + time).length === 10) {
|
*/
|
||||||
time = parseInt(time) * 1000
|
export function formatTime(time, option) {
|
||||||
} else {
|
if (('' + time).length === 10) {
|
||||||
time = +time
|
time = parseInt(time) * 1000
|
||||||
}
|
} else {
|
||||||
const d = new Date(time)
|
time = +time
|
||||||
const now = Date.now()
|
}
|
||||||
|
const d = new Date(time)
|
||||||
const diff = (now - d) / 1000
|
const now = Date.now()
|
||||||
|
|
||||||
if (diff < 30) {
|
const diff = (now - d) / 1000
|
||||||
return '刚刚'
|
|
||||||
} else if (diff < 3600) {
|
if (diff < 30) {
|
||||||
// less 1 hour
|
return '刚刚'
|
||||||
return Math.ceil(diff / 60) + '分钟前'
|
} else if (diff < 3600) {
|
||||||
} else if (diff < 3600 * 24) {
|
// less 1 hour
|
||||||
return Math.ceil(diff / 3600) + '小时前'
|
return Math.ceil(diff / 60) + '分钟前'
|
||||||
} else if (diff < 3600 * 24 * 2) {
|
} else if (diff < 3600 * 24) {
|
||||||
return '1天前'
|
return Math.ceil(diff / 3600) + '小时前'
|
||||||
}
|
} else if (diff < 3600 * 24 * 2) {
|
||||||
if (option) {
|
return '1天前'
|
||||||
return parseTime(time, option)
|
}
|
||||||
} else {
|
if (option) {
|
||||||
return (
|
return parseTime(time, option)
|
||||||
d.getMonth() +
|
} else {
|
||||||
1 +
|
return (
|
||||||
'月' +
|
d.getMonth() +
|
||||||
d.getDate() +
|
1 +
|
||||||
'日' +
|
'月' +
|
||||||
d.getHours() +
|
d.getDate() +
|
||||||
'时' +
|
'日' +
|
||||||
d.getMinutes() +
|
d.getHours() +
|
||||||
'分'
|
'时' +
|
||||||
)
|
d.getMinutes() +
|
||||||
}
|
'分'
|
||||||
}
|
)
|
||||||
|
}
|
||||||
/**
|
}
|
||||||
* @param {string} url
|
|
||||||
* @returns {Object}
|
// 获取当前月份的第一天
|
||||||
*/
|
|
||||||
export function param2Obj(url) {
|
export function getCurrentMonthFirst() {
|
||||||
const search = decodeURIComponent(url.split('?')[1]).replace(/\+/g, ' ')
|
const date = new Date();
|
||||||
if (!search) {
|
|
||||||
return {}
|
date.setDate(1);
|
||||||
}
|
|
||||||
const obj = {}
|
var month = parseInt(date.getMonth() + 1);
|
||||||
const searchArr = search.split('&')
|
|
||||||
searchArr.forEach(v => {
|
var day = date.getDate();
|
||||||
const index = v.indexOf('=')
|
|
||||||
if (index !== -1) {
|
if (month < 10) {
|
||||||
const name = v.substring(0, index)
|
month = '0' + month
|
||||||
const val = v.substring(index + 1, v.length)
|
|
||||||
obj[name] = val
|
}
|
||||||
}
|
|
||||||
})
|
if (day < 10) {
|
||||||
return obj
|
day = '0' + day
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return date.getFullYear() + '-' + month + '-' + day;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取当前月份的最后一天
|
||||||
|
|
||||||
|
export function getCurrentMonthLast() {
|
||||||
|
const date = new Date();
|
||||||
|
|
||||||
|
var currentMonth = date.getMonth();
|
||||||
|
|
||||||
|
var nextMonth = ++currentMonth;
|
||||||
|
|
||||||
|
var nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1);
|
||||||
|
|
||||||
|
var oneDay = 1000 * 60 * 60 * 24;
|
||||||
|
|
||||||
|
var lastTime = new Date(nextMonthFirstDay - oneDay);
|
||||||
|
|
||||||
|
var month = parseInt(lastTime.getMonth() + 1);
|
||||||
|
|
||||||
|
var day = lastTime.getDate();
|
||||||
|
|
||||||
|
if (month < 10) {
|
||||||
|
month = '0' + month
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if (day < 10) {
|
||||||
|
day = '0' + day
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return date.getFullYear() + '-' + month + '-' + day;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} url
|
||||||
|
* @returns {Object}
|
||||||
|
*/
|
||||||
|
export function param2Obj(url) {
|
||||||
|
const search = decodeURIComponent(url.split('?')[1]).replace(/\+/g, ' ')
|
||||||
|
if (!search) {
|
||||||
|
return {}
|
||||||
|
}
|
||||||
|
const obj = {}
|
||||||
|
const searchArr = search.split('&')
|
||||||
|
searchArr.forEach(v => {
|
||||||
|
const index = v.indexOf('=')
|
||||||
|
if (index !== -1) {
|
||||||
|
const name = v.substring(0, index)
|
||||||
|
const val = v.substring(index + 1, v.length)
|
||||||
|
obj[name] = val
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return obj
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in new issue