js常用util

2018-06-24 00:41:19来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

/** 日期格式化 */
Date.prototype.Format = function(format) {
 var o = {
  "M+" : this.getMonth() + 1, // month
  "d+" : this.getDate(), // day
  "H+" : this.getHours(), // hour
  "m+" : this.getMinutes(), // minute
  "s+" : this.getSeconds(), // second
  "q+" : Math.floor((this.getMonth() + 3) / 3), // quarter
  "S" : this.getMilliseconds()
 // millisecond
 };

 if (/(y+)/.test(format)) {
  format = format.replace(RegExp.$1, (this.getFullYear() + "")
    .substr(4 - RegExp.$1.length));
 }

 for ( var k in o) {
  if (new RegExp("(" + k + ")").test(format)) {
   format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k]
     : ("00" + o[k]).substr(("" + o[k]).length));
  }
 }
 return format;
};

/** 数字格式化 */
function numberFormat(s, n) {
 n = n > 0 && n <= 20 ? n : 2;
 var m = parseFloat(s + "") < 0 ? "-" : "";
 s = parseFloat((Math.abs(s) + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
 var l = s.split(".")[0].split("").reverse(), r = s.split(".")[1];
 t = "";
 for (i = 0; i < l.length; i++) {
  t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
 }
 return m + t.split("").reverse().join("") + "." + r;
}

/** 获取当前周的起止日期 */
Date.prototype.getCurrentWeek = function(date) {
 var today;
 if (null == date || undefined == date || date == "") {
  today = this;
 } else {
  today = date;
 }
 var week = today.getDay();
 if (week == 0) {
  week = 7
 }
 var startDate = new Date(today.valueOf() - (week - 1) * 24 * 60 * 60 * 1000);
 var endDate = new Date(startDate.valueOf() + 6 * 24 * 60 * 60 * 1000);
 var resultObj = new Object();
 resultObj['startDate'] = startDate.Format("yyyy-MM-dd");
 resultObj['endDate'] = endDate.Format("yyyy-MM-dd");
 return resultObj;
};

/** 获取当前月的起止日期 */
Date.prototype.getCurrentMonth = function(date) {
 var today;
 if (null == date || undefined == date || date == "") {
  today = this;
 } else {
  today = date;
 }
 var year = today.getFullYear();
 var month = today.getMonth();
 var firstDay = new Date(year, month, 1);
 if (month == 11) {
  month = 0;
  year++;
 } else {
  month++;
 }
 var nextDay = new Date(year, month, 1);
 var resultObj = new Object();
 resultObj['startDate'] = firstDay.Format("yyyy-MM-dd");
 resultObj['endDate'] = new Date(nextDay.valueOf() - 24 * 60 * 60 * 1000)
   .Format("yyyy-MM-dd");
 return resultObj;
};

/**减一天标准格式(yyyy-MM-dd)  */
function reduceOneDay(date) {
 var dateTime = new Date(Date.parse(date.replace(/-/g, "/")));
 var result = new Date(dateTime.getTime() - 24*60*60*1000);
 return result.Format("yyyy-MM-dd");
}

/** 通过时间返回中文星期 */
function getChineseWeek(date) {
 var d = new Date(date.replace(/-/g, "/"));
 var week = d.getDay();
 var str;
 if (week == 0) {
  str = "日";
 } else if (week == 1) {
  str = "一";
 } else if (week == 2) {
  str = "二";
 } else if (week == 3) {
  str = "三";
 } else if (week == 4) {
  str = "四";
 } else if (week == 5) {
  str = "五";
 } else if (week == 6) {
  str = "六";
 }
 return "星期" + str;
}

/**
 * 将对象转成get请求参数形式
 * 如?param1=value1&param2=value2
 */
function changeParamToStr(param) {
 var result = "";
 if (param) {
  for ( var i in param) {
   result += "&" + i + "=" + param[i];
  }
 }
 if (result != "") {
  result = "?" + result.substring(1);
 }
 return result;
}

/**处理等待遮罩层*/
function onLoading(selector, msg) {
    var offset = $(selector).offset();

    var dialog = $("<div class='diy-msk'></div>").dialog({
  modal : true
 });
 $(dialog).parent().css("display", "none");
 $(dialog).parent().next().css("display", "none");
 
    $("<div class='datagrid-mask-msg'></div>").html(!msg ? "正在处理中,请稍候..." : msg)
        .appendTo(selector).css({
            display : "block",
            left : ($(document.body).outerWidth(true) - 190) / 2,
            top : offset.top + ($(selector).height() - 45) / 2
        });
}

/**移除等待遮罩层*/
function removeLoad(selector) {
 $(selector + " .panel window").remove();
 $(selector + " .window-shadow").remove();
 $(selector + " .window-mask").remove();
 $(selector + " .datagrid-mask-msg").remove();
}

/**创建上传窗口 公共方法,chunk 是否分割大文件,callBack 上传成功之后的回调*/
function Uploader(root, param,callBack){
 var addWin = $("<div style='overflow: hidden;'/>");
 var upladoer = $("<iframe/>");
 upladoer.attr({"src":root+"/plupload/uploader.jsp"+changeParamToStr(param),
   width:"100%",height:"100%",frameborder:"0",scrolling:"no"});
 addWin.window({
  title:"上传文件",
  height:350,
  width:550,
  minimizable:false,
  modal:true,
  collapsible:false,
  maximizable:false,
  resizable:false,
  content:upladoer,
  onClose:function(){
   var fw = GetFrameWindow(upladoer[0]);
   var files = fw.files;
   $(this).window("destroy");
   callBack.call(this,files);
  },
  onOpen:function(){
   var target = $(this);
   setTimeout(function(){
    var fw = GetFrameWindow(upladoer[0]);
    fw.target = target;
   },100);
  }
 });
}

/**根据iframe对象获取iframe的window对象*/
function GetFrameWindow(frame){
 return frame && typeof(frame)=="object" && frame.tagName == "IFRAME" && frame.contentWindow;
}

/**到年日期格式*/
function yearFormatter(date) {
 return date.Format("yyyy");
}
/**到年解析日期*/
function yearParser(date) {
 if(date != "") {
  date += "-01-01";
  return new Date(Date.parse(date.replace(/-/g, "/")));
 } else {
  return new Date(); 
 }
}

/**到月日期格式*/
function monthFormatter(date) {
 return date.Format("yyyy-MM");
}
/**到月解析日期*/
function monthParser(date) {
 if(date != "") {
  date += "-01";
  return new Date(Date.parse(date.replace(/-/g, "/")));
 } else {
  return new Date(); 
 }
}

/**处理datagrid合并*/
function sks_handleMerge(id, obj) {
 for(var i in obj) {
  var arr = obj[i];
  if(null != arr && arr.length > 0) {
   for(var j in arr) {
    $(id).datagrid("mergeCells", {
     index : arr[j].index,
     type : "footer",
     field : i,
     rowspan : arr[j].rowspan,
     colspan : 0
    });
   }
  }
 }
}

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:angular-utils-ui-breadcrumbs使用心得

下一篇:【前端】javascript+jQuery实现360开机时间显示效果