1. 截取地址栏里携带的参数
/**
* 解析地址
* @param url
* @param name
* @returns {string|null}
*/
export function getQueryString(url, name) {
let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
let r = ('?' + url.split('?')[1]).substr(1).match(reg);
if (r != null) return unescape(r[2]);
return null;
}
具体使用:let token = getQueryString(window.location.href, 'token')
2. 时间转换工具
①
/**
* 时间戳转换工具
* @param timestamp 时间戳
* @returns {string} yy-mm-dd
*/
export function formatDate(timestamp) {
if (!timestamp) {
return "";
}
let date = new Date(timestamp);
let month = date.getMonth() + 1;
let strDate = date.getDate();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
let dateStr = date.getFullYear() + '-' + month + '-' + strDate;
return dateStr;
}
②
/**
* 时间戳转换工具
* @param timestamp 毫秒时间戳
* @returns {string} yy-mm-dd-hh-mm-ss
*/
export function formatTime(timestamp) {
if (!timestamp) {
return "";
}
let date = new Date(timestamp);
let year = date.getFullYear();
let month = date.getMonth() + 1;
if (month >= 1 && month <= 9) {
month = "0" + month;
}
let strDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
let hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
let min = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
let sec = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
let dateStr = year + '-' + month + '-' + strDate + " " + hours + ':' + min + ':' + sec;
return dateStr;
}
③
/**
* 时间戳转换工具
* @param timestamp 毫秒时间戳
* @returns {string} yy-mm-dd-hh-mm-ss
*/
export function formatTime(timestamp) {
if (!timestamp) {
return "";
}
let date = new Date(timestamp);
let month = date.getMonth() + 1;
let strDate = date.getDate();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
let dateStr = date.getFullYear() + '-' + month + '-' + strDate +
" " + (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ':' +
(date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()) +
':' + (date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds());
return dateStr;
}
④
/**
* 获取当前时间
* @returns {string} yyyy-MM-dd格式
*/
export function newDate() {
let date = new Date();
let month = date.getMonth() + 1;
let strDate = date.getDate();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
let dateStr = date.getFullYear() + '-' + month + '-' + strDate;
return dateStr;
}
⑤
/**
* n天前的时间
* @param n 时间差
* @returns {string} yyyy-MM-dd格式
*/
export function beforeDate(n) {
let date = new Date().getTime();
date = date - n * 24 * 60 * 60 * 1000;
return formatDate(date);
}
⑥
Date.prototype.format = function (fmt) {
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
export default Date
// 用法 const dd = new Date(date)
obj.visitstartdate = dd.format("yyyy-MM-dd")
var begin = new Date();
var end = new Date();
new Date(begin.setMonth((new Date().getMonth() - 1)));
var begintime = begin.format("yyyy-MM-dd");
var endtime = end.format("yyyy-MM-dd");
$('#txtBeginVisitTime').val(begintime);
$('#txtEndVisitTime').val(endtime);
3. 字符串的截取
/**
* 字符串截取
* @param str 字符串
* @param n 截至字符的下标
* @returns {*}
*/
export function subString(str, n) {
if (!n) return str;
let sign = str.length <= n ? '' : '...';
return str.substring(0, n) + sign;
}