原生javascript操作cookie和日期格式的通用方法
function setCookie(name,value)
{
var Days = 1;
var exp = new Date();
exp.setTime(exp.getTime() + Days*24*60*60*1000);
exp.setHours(0);
exp.setMinutes(0);
exp.setSeconds(0);
exp.setMilliseconds(0);
document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString()+";domain=.jin10.com;path=/";
}
function getCookie(name)
{
var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
if(arr=document.cookie.match(reg))
return unescape(arr[2]);
else
return null;
}
function delCookie(name)
{
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval=getCookie(name);
if(cval!=null) {
document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString()+";domain=.jin10.com;path=/";
}
}
Date.prototype.format =function(format)
{
var o = {
"M+" : this.getMonth()+1,
"d+" : this.getDate(),
"h+" : this.getHours(),
"m+" : this.getMinutes(),
"s+" : this.getSeconds(),
"q+" : Math.floor((this.getMonth()+3)/3),
"S" : this.getMilliseconds()
}
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;
}
如果您觉得本文的内容对您的学习有所帮助:
关键字:
javascript 正则表达式 获取参数值 function 对象是否为空 cookie setCookie getCookie delCookie Date.prototype.format