常用JS工具方法
七月 04, 2018
常用JS工具方法,可以直接在JS开发中拿来就用。so easy.
- 日期时间显示格式化
- 获取页面链接参数
- 文件大小显示格式化
- 获取指定的时间间隔
日期时间显示格式化
//common.js
//日期时间显示 格式化 例:2016-12-12 12:12:12
let datetimeFormat = function(data, fmt){
if(!data){
return '';
}else{
var dateData = new Date(data);
var o = {
"Y+" : dateData.getFullYear(), //年份
"M+" : dateData.getMonth()+1, //月份
"d+" : dateData.getDate(), //日
"h+" : dateData.getHours(), //小时
"m+" : dateData.getMinutes(), //分
"s+" : dateData.getSeconds(), //秒
"q+" : Math.floor((dateData.getMonth()+3)/3), //季度
"S" : dateData.getMilliseconds() //毫秒
};
if(/(y+)/.test(fmt)){
fmt=fmt.replace(RegExp.$1, (dateData.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;
}
};
module.exports = {
datetimeFormat
}
//使用
import { datetimeFormat } from 'common';
createTime = datetimeFormat(createTime, "Y-MM-dd hh:mm:ss");
获取页面链接参数
//common.js
let getUrlParam = function(name) {
let str = window.location.href;
let num = str.indexOf("?")
str = str.substr(num+1);
let arr = str.split("&"), params = {}; //各个参数放到数组里
for(let i=0; i<arr.length; i++){
num = arr[i].indexOf("=");
if(num>0){
let key = arr[i].substring(0,num);
let value = arr[i].substr(num+1);
params[key] = value;
}
}
if(params[name]){
return params[name];
}else{
return null;
}
}
module.exports = {
getUrlParam
}
//使用
import { getUrlParam } from 'common';
getUrlParam('id')
文件大小显示格式化
//common.js
//文件大小显示 格式化 例:10KB,200MB,1020GB
let fileSizeFormat = function(fileSize){
let formatFileSize = '0B';
if(fileSize){
if(fileSize<1024){ formatfilesize="fileSize+'B';" }else{="" let="" ksize="fileSize/1024;" if(ksize<1024){="" 'kb';="" msize="kSize/1024;" if(msize<1024){="" 'mb';="" gsize="mSize/1024;" if(gsize<1024){="" 'gb';="" tsize="gSize/1024;" if(tsize<1024){="" 'tb';="" }="" return="" formatfilesize;="" };="" module.exports="{" filesizeformat="" 使用="" import="" {="" from="" 'common';="" filesizeformat(filesize)="" <="" code="">
1024){>
获取指定的时间间隔
//common.js
import moment from 'moment';
let fixedZero = function(val) {
return val * 1 < 10 ? `0${val}` : val;
}
let getTimeDistance = function(type) {
const now = new Date();
const oneDay = 1000 * 60 * 60 * 24;
if (type === 'today') {
now.setHours(0);
now.setMinutes(0);
now.setSeconds(0);
return [moment(now), moment(now.getTime() + (oneDay - 1000))];
}
if (type === 'week') {
let day = now.getDay();
now.setHours(0);
now.setMinutes(0);
now.setSeconds(0);
if (day === 0) {
day = 6;
} else {
day -= 1;
}
const beginTime = now.getTime() - (day * oneDay);
return [moment(beginTime), moment(beginTime + ((7 * oneDay) - 1000))];
}
if (type === 'month') {
const year = now.getFullYear();
const month = now.getMonth();
const nextDate = moment(now).add(1, 'months');
const nextYear = nextDate.year();
const nextMonth = nextDate.month();
return [moment(`${year}-${fixedZero(month + 1)}-01 00:00:00`), moment(moment(`${nextYear}-${fixedZero(nextMonth + 1)}-01 00:00:00`).valueOf() - 1000)];
}
if (type === 'year') {
const year = now.getFullYear();
return [moment(`${year}-01-01 00:00:00`), moment(`${year}-12-31 23:59:59`)];
}
}
module.exports = {
getTimeDistance
}
//使用
import { getTimeDistance } from 'common';
getTimeDistance('week');
查看评论