JavaScript SNIPPETS

function getTime(/** timestamp=0 **/) {
  var ts = arguments[0] || 0;
  var t,y,m,d,h,i,s;
  t = ts ? new Date(ts*1000) : new Date();
  y = t.getFullYear();
  m = t.getMonth()+1;
  d = t.getDate();
  h = t.getHours();
  i = t.getMinutes();
  s = t.getSeconds();
  // 可根据需要在这里定义时间格式
  return y+'-'+(m<10?'0'+m:m)+'-'+(d<10?'0'+d:d)+' '+(h<10?'0'+h:h)+':'+(i<10?'0'+i:i)+':'+(s<10?'0'+s:s);
}

alert(getTime(1404276301));

cookie caozuo

function getCookie(c_name) {
	if (document.cookie.length > 0) {
		c_start = document.cookie.indexOf(c_name + "=")
		if (c_start != -1) {
			c_start = c_start + c_name.length + 1
			c_end = document.cookie.indexOf(";", c_start)
			if (c_end == -1)
				c_end = document.cookie.length
			return unescape(document.cookie.substring(c_start, c_end))
		}
	}
	return ""
}

function setCookie(c_name, value, expiredays) {
	var exdate = new Date()
	exdate.setDate(exdate.getDate() + expiredays)
	document.cookie = c_name
			+ "="
			+ escape(value)
			+ ((expiredays == null) ? "" : ";expires="
					+ exdate.toGMTString())
}

 

 

字符串格式替换

function resetiUin(String){
	
	 return String.replace(/(\d{2})\d{2}(\d{1,10})/, "$1****$2");
}
<script type="text/javascript">
    //替换字符串  
    function Replace(str, from, to) {
        return str.split(from).join(to);
    }
    // 日期类型格式成指定的字符串
    function FormatDate(date, format) {
        format = Replace(format, "yyyy", date.getFullYear());
        format = Replace(format, "MM", GetFullMonth(date));
        format = Replace(format, "dd", GetFullDate(date));
        format = Replace(format, "HH", GetFullHour(date));
        return format;
    }
    //js日期字符串转换成日期类型
    function parseDate(dateStr) {
        return new Date(Replace(dateStr, "-", "/"));
    }
    //增加月  
    function AddMonths(date, value) {
        date.setMonth(date.getMonth() + value);
        return date;
    }
    //增加天  
    function AddDays(date, value) {
        date.setDate(date.getDate() + value);
        return date;
    }
    //增加时
    function AddHours(date, value) {
        date.setHours(date.getHours() + value);
        return date;
    }
    //返回月份(两位数)  
    function GetFullMonth(date) {
        var v = date.getMonth() + 1;
        if (v > 9) return v.toString();
        return "0" + v;
    }
 
    //返回日(两位数)  
    function GetFullDate(date) {
        var v = date.getDate();
        if (v > 9) return v.toString();
        return "0" + v;
    }
    //返回时(两位数)
    function GetFullHour(date) {
        var v = date.getHours();
        if (v > 9) return v.toString();
        return "0" + v;
    }
    //比较两个时间
    function compareDate() {
        var mydate = AddDays(parseDate("2012-08-23"), 1);
        var nowdate = new Date();
        if (nowdate.getTime() < mydate.getTime()) {
            return FormatDate(nowdate, "yyyy-MM-dd");
        }
        return FormatDate(mydate, "yyyy-MM-dd");
    }
</script>

 

 

获取参数

function getParameter (name) {
	var _search = document.location.search;
	var pattern = new RegExp("[?&]" + name + "\=([^&]+)", "g");
	var matcher = pattern.exec(_search);
	var items = null;
	if (null != matcher) {
		items = decodeURIComponent(matcher[1]);
	}
	return items;
}

 

发表评论