aqyj/utils/util.js

91 lines
2.3 KiB
JavaScript

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()
return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute].map(formatNumber).join(':')
}
const formatNumber = n => {
n = n.toString()
return n[1] ? n : '0' + n
}
let getQueryString = function (url, name) {
console.log("url = " + url)
console.log("name = " + name)
var reg = new RegExp('(^|&|/?)' + name + '=([^&|/?]*)(&|/?|$)', 'i')
var r = url.substr(1).match(reg)
if (r != null) { console.log("r = " + r)
console.log("r[2] = " + r[2])
return r[2] } return null; }
/**
* 格式化秒
* @param int value 总秒数
* @return string result 格式化后的字符串
*/
function formatSeconds(value) {
var theTime = parseInt(value);// 需要转换的时间秒
var theTime1 = 0;// 分
var theTime2 = 0;// 小时
var theTime3 = 0;// 天
if (theTime > 60) {
theTime1 = parseInt(theTime / 60);
theTime = parseInt(theTime % 60);
if (theTime1 > 60) {
theTime2 = parseInt(theTime1 / 60);
theTime1 = parseInt(theTime1 % 60);
if (theTime2 > 24) {
//大于24小时
theTime3 = parseInt(theTime2 / 24);
theTime2 = parseInt(theTime2 % 24);
}
}
}
var result = '';
if (theTime > 0) {
result = "" + parseInt(theTime) + "秒";
}
if (theTime1 > 0) {
result = "" + parseInt(theTime1) + "分" + result;
}
if (theTime2 > 0) {
result = "" + parseInt(theTime2) + "小时" + result;
}
if (theTime3 > 0) {
result = "" + parseInt(theTime3) + "天" + result;
}
return result;
}
function toSubString(value) {
if (value.length>=14){
var str = value.substring(0, 14)
return str + "..."
} else {
return value
}
}
module.exports = {
getQueryString: getQueryString,
formatTime: formatTime,
formatTime2:formatTime2,
formatSeconds:formatSeconds,
toSubString: toSubString
}