js常用util
2018-06-24 00:41:19来源:未知 阅读 ()
/** 日期格式化 */
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¶m2=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
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- javascript 中关于array的常用方法详解 2020-03-16
- JS实现标签页切换效果 2020-03-12
- javascript的常用方法 2020-03-08
- javascript数组常用的方法 2019-12-18
- 总结js常用数组的操作方法 2019-12-13
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash