123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- var numToArray = function (num) {
- var arr = [];
- for (var i = 0; i < num; i++) {
- arr[i] = i;
- }
- return arr;
- }
- var formatNumber = function (n) {
- n = n.toString()
- return n[1] ? n : '0' + n
- }
- /**
- * 时间戳格式化时间
- */
- var formatTime = function (datetime) {
- if (!datetime) return '';
- var date = getDate(datetime * 1000);
- var year = date.getFullYear()
- var month = date.getMonth() + 1
- var day = date.getDate()
- var hour = date.getHours()
- var minute = date.getMinutes()
- var second = date.getSeconds()
- return year + '-' + formatNumber(month) + '-' + formatNumber(day) + ' ' + formatNumber(hour) + ':' + formatNumber(minute) + ':' + formatNumber(second);
- }
- /**
- * 时间戳格式化时间 不带秒
- */
- var formatTimeNoSecond = function (datetime) {
- if (!datetime) return '';
- var date = getDate(datetime * 1000);
- var year = date.getFullYear()
- var month = date.getMonth() + 1
- var day = date.getDate()
- var hour = date.getHours()
- var minute = date.getMinutes()
- var second = date.getSeconds()
- return year + '-' + formatNumber(month) + '-' + formatNumber(day) + ' ' + formatNumber(hour) + ':' + formatNumber(minute);
- }
- // 时间戳格式化 返回日期
- var formatGetDate = function (datetime) {
- var date = getDate(datetime * 1000);
- var year = date.getFullYear()
- var month = date.getMonth() + 1
- var day = date.getDate()
- return year + '-' + formatNumber(month) + '-' + formatNumber(day);
- }
- /**
- * 隐藏字符串 多余字符使用*替代
- */
- var maskStr = function (str, replace, len) {
- if (typeof str != 'string')
- return '';
- if(str.length < len) return str;
- return str.substring(0, len) + replace + replace + replace;
- }
- /**
- * 将字符串分割成数组
- */
- var splitx = function (str, splitstr) {
- if (typeof str != 'string')
- return [];
- return str.split(splitstr);
- }
- var toFixed = function(num, n) {
- return Math.floor(num * Math.pow(10, n)) / Math.pow(10, n)
- }
- var showMoney = function(m) {
- var _n = m / 1000
- if(_n <=1 ) {
- return m
- }
- if (_n > 1 && _n < 10) {
- return _n.toFixed(1) + '千'
- }
- _n = m / 10000
- return _n.toFixed(1) + '万'
- }
- function formatSeconds(value) {
- var theTime = parseInt(value);// 需要转换的时间秒
- var theTime1 = 0;// 分
- var theTime2 = 0;// 小时
- var theTime3 = 0;// 天
- if (theTime === 0) return [parseInt(theTime), "sec", 'S'];
- 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) , "sec", 'S'];
- }
- if (theTime1 > 0) {
- result = [parseInt(theTime1), "min", 'M'].concat(result);
- }
- if (theTime2 > 0) {
- result = [parseInt(theTime2), "hour", 'H'].concat(result);
- }
- if (theTime3 > 0) {
- result = [parseInt(theTime3), "day", 'D'].concat(result);
- }
- return result;
- }
- var formatHourSeconds = function (time) {
- var sec = parseInt(time % 60);
- var min = parseInt(parseInt(time / 60) % 60);
- var hour = parseInt(parseInt(time / 60) / 60);
- return formatNumber(hour) + ':' + formatNumber(min) + ':' + formatNumber(sec);
- }
- var filterHalf = function (l) {
- return l.filter(function (p, i) { return i % 2 == 1});
- }
- var max = function (arr) {
- return [...arr].sort(function (a, b) { return b - a })[0];
- }
- var min = function (arr) {
- return [...arr].sort(function (a, b) { return a - b })[0];
- }
- var getOption = function (option, value) {
- return option.filter(function (p, i) { return p.value == value })[0]
- }
- var getOptionIndex = function (option, value) {
- var i
- for (i = 0; i < option.length; i++) {
- if (option[i].value == value) {
- return i
- }
- }
- }
- module.exports = {
- numToArray: numToArray,
- formatTime: formatTime,
- formatGetDate: formatGetDate,
- maskStr: maskStr,
- splitx: splitx,
- toFixed: toFixed,
- showMoney: showMoney,
- formatSeconds: formatSeconds,
- formatHourSeconds: formatHourSeconds,
- filterHalf: filterHalf,
- max: max,
- min: min,
- getOption: getOption,
- getOptionIndex: getOptionIndex,
- formatTimeNoSecond:formatTimeNoSecond
- }
|