123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- export function getDay (day,haveHours){
- //day为0表示今天的日期 -1为昨天 haveHours存在表示要时分秒 haveHours:true,false
- var today = new Date();
- var targetday_milliseconds=today.getTime() + 1000*60*60*24*day;
- today.setTime(targetday_milliseconds); //注意,这行是关键代码
- var tYear = today.getFullYear();
- var tMonth = today.getMonth();
- var tDate = today.getDate();
- var hours = today.getHours();
- var minutes = today.getMinutes(); //分
- var seconds = today.getSeconds(); //秒
- tMonth = doHandleMonth(tMonth + 1);
- tDate = doHandleMonth(tDate);
- if(haveHours){
- if(day == 0){
- return {
- 'now':tYear+"-"+tMonth+"-"+tDate+' ' + hours + ':' + minutes + ':' + seconds,
- 'zero':tYear+"-"+tMonth+"-"+tDate+' ' + '00:00:00'
- }
- }else{
- return tYear+"-"+tMonth+"-"+tDate+' ' + hours + ':' + minutes + ':' + seconds
- }
- }else{
- return tYear+"-"+tMonth+"-"+tDate
- }
- function doHandleMonth(month){
- var m = month;
- if(month.toString().length == 1){
- m = "0" + month;
- }
- return m;
- }
- }
- export function NumberHandle (value,numberDigit) {//数值小数点处理 ①5997;②8.1w;③2489kw;④4.2亿 且保留一位小数
- if(typeof parseFloat(value) === 'number' && !isNaN(value)){
- //判断是否是数值类型
- if((value >= 10000 && value < 100000000) || (value <= -10000 && value > -100000000)){
- if(hasDot(value/10000000,numberDigit)==10){
- return '1亿'
- }else{
- return hasDot(value/10000,numberDigit) + 'w'
- }
- }else if(value >= 100000000 || value <= -100000000){
- return hasDot(value/100000000,numberDigit) + '亿'
- }else{
- if(value==undefined){
- return '-'
- }else{
- return hasDot(value,numberDigit)
- }
- }
- }else{
- if(value==undefined){
- return '-'
- }else{
- return value
- }
- }
- }
- function hasDot(num,numberDigit){
- //有小数点就保留一个小数,没有就直接返回 ,默认保留1位小数
- var digit = 1;
- if(numberDigit){
- digit = numberDigit
- }
- return formatNum((num + '').indexOf('.') != -1 ? parseFloat(Number(num).toFixed(digit)) : num);
- }
- //复制
- export function copy(data,cb) {
- var copyBox = document.createElement("p");
- copyBox.innerText = data;
- //把创建后的子元素追加到父级元素中
- document.body.appendChild(copyBox)
- if (document.body.createTextRange) {
- //createTextRange是用在IE中的
- var range = document.body.createTextRange();
- range.moveToElementText(copyBox);
- range.select();
- } else if (window.getSelection) {
- var selection = window.getSelection();
- var range = document.createRange();
- range.selectNodeContents(copyBox);
- selection.removeAllRanges();
- selection.addRange(range);
- document.execCommand("Copy")
- }
- copyBox.remove();
- cb?cb():''
- }
- // 检测是否全是中文
- export function isChn(str){
- var reg = /^[\u4E00-\u9FA5]+$/;
- if(!reg.test(str)){
- // alert("不是中文");
- return false;
- }
- //alert("中文");
- return true;
- }
- export function formatNum (str){
- if (typeof parseFloat(str) === 'number' && !isNaN(str) && str != null) {
- //判断是否是数值类型
- var newStr = "";
- var count = 0;
- str = Number(str).toFixed(2)
- str = parseFloat(str)
- str = str.toString();
- if (str.indexOf(".") == -1) {
- for (var i = str.length - 1; i >= 0; i--) {
- if (count % 3 == 0 && count != 0 && str.charAt(i) != '-') {
- newStr = str.charAt(i) + "," + newStr;
- } else {
- newStr = str.charAt(i) + newStr;
- }
- count++;
- }
- str = newStr; //自动补小数点后两位
- } else {
- for (var i = str.indexOf(".") - 1; i >= 0; i--) {
- if (count % 3 == 0 && count != 0 && str.charAt(i) != '-') {
- newStr = str.charAt(i) + "," + newStr;
- } else {
- newStr = str.charAt(i) + newStr; //逐个字符相接起来
- }
- count++;
- }
- str = newStr + str.substring(str.indexOf("."), str.length);
- }
- }
- return str
- }
- //秒转化为时
- export function formateHours(endTime){
- if(endTime){
- let secondTime = parseInt(endTime)//将传入的秒的值转化为Number
- let h =0// 初始化小时
- let result=''
- h = parseFloat((secondTime/3600).toFixed(1))
- result=h+"h"
- return result
- }else{
- return "0h"
- }
- }
|