1、js日期格式化:
使用方式:new Date(v).format('yyyy-MM-dd hh:mm');
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; }
2、过滤HTML,自动提取摘要功能
//HTML过滤 function removeHTMLTag(str) { str = str.replace(/<\/?[^>]*>/g,""); //去除HTML tag str = str.replace(/[ | ]*\n/g,'\n'); //去除行尾空白 str = str.replace(/ /ig,"");//去掉  str = str.replace(/[\r\n]/g,""); //去掉回车换行 str = str.replace(/\s+/g, ""); //去掉字符串所有空格 return str; }
3、js清除File控件
使用方法: <input id="file1" type="file" /> <input type="button" value="清空" οnclick="cleanFile('file1')" />
<script> function cleanFile(id){ var _file = document.getElementById(id); if(_file.files){ _file.value = ""; }else{ if (typeof _file != "object"){ return null; } var _span = document.createElement("span"); _span.id = "__tt__"; _file.parentNode.insertBefore(_span,_file); var tf = document.createElement("form"); tf.appendChild(_file); document.getElementsByTagName("body")[0].appendChild(tf); tf.reset(); _span.parentNode.insertBefore(_file,_span); _span.parentNode.removeChild(_span); _span = null; tf.parentNode.removeChild(tf); } } </script>
4、js兼容ie和火狐的回车键事件
<script type="text/javascript"> document.οnkeypress=function(e) { var code; if (!e) { var e=window.event; } if(e.keyCode) { code=e.keyCode; } else if(e.which) { code = e.which; } if(code==13) { ///这里是调用执行的方法 /// return false; } } </script>
5、获取当前时期格式如0000-00-00
function showdate(){ var today=new Date(); date=today.getDate(); month=today.getMonth(); month=month+1; if(month<=9) month="0"+month; year=today.getYear(); var nowDate=year+'-'+month+'-'+date; return nowDate; }
6、获得当地时间
function TimeDemo(){ var d, s=""; var c = ":"; d = new Date(); s += d.getHours() + c; s += d.getMinutes() + c; s += d.getSeconds() + c; s += d.getMilliseconds(); return s; }
7、判断两个标准格式日期的大小并返回较大的日期0000-00-00
function compareTwoDate(date1,date2){
date1=date1.substring(0,10);
date2=date2.substring(0,10);
if(date1<date2){
return date2;
}else if(date1>date2){
return date1;
}
return date1;
}
8、得到某个日期N天之后的日期
afterDay=function(objDate,dayNum){ var arr=new Array; //var objDate='2010-04-13'; objDate=objDate.substring(0,10); //alert(objDate+"/obj/"); arr=objDate.split("-"); //alert(arr[0]+"==year"); //alert(Number(arr[1])+"==month"); //alert(arr[2]+"==day"); year=arr[0]; month=arr[1]; day=arr[2]; var dat = new Date(Number(year),Number(month)-1,Number(day)); var daysec=dat.getTime()+dayNum*24*3600*1000; var tempdate=new Date(daysec).toLocaleString().split(" ")[0];//[)左闭右开 year=tempdate.substring(0,tempdate.indexOf("年")); month=tempdate.substring(tempdate.indexOf("年")+1,tempdate.indexOf("月")); if(month.toString().length==1){ month="0"+month; } day=tempdate.substring(tempdate.indexOf("月")+1,tempdate.indexOf("日")); if(day.toString().length==1){ day="0"+day; } return year+"-"+month+"-"+day; return tempdate; }