javascript DateTimer 时间控件 生肖 星座

DateTimer = function(elem_id, current) {
			var local = new Date().getTime();
			this.current = current;
			this.diff = current - local;
			this.elem = document.getElementById(elem_id);
		}

		DateTimer.prototype.start = function() {
			var _self = this;
			var timerID = setTimeout(function() {
				_self.start()
			}, 1000);
			if (this.elem == null)
				return;
			if (this.elem == null)
				return;
			var value = new Date().getTime() + this.diff;
			var d = new Date(value);
			var hours = d.getHours();
			var minutes = d.getMinutes();
			var seconds = d.getSeconds();
			if (hours < 10)
				hours = '0' + hours;
			if (minutes < 10)
				minutes = '0' + minutes;
			if (seconds < 10)
				seconds = '0' + seconds;
			var innerstr = hours + ':' + minutes + ':' + seconds;
			this.elem.innerHTML = innerstr;
		}

		DateTimer.prototype.showTime = function() {
			if (this.elem.size() == 0)
				return;
			var ms = this.current;
			var s = h = m = 0;
			s = Math.floor(ms / 1000);
			if (s >= 60) {
				m = Math.floor(s / 60);
				s = s - m * 60
			}
			if (m >= 60) {
				h = Math.floor(m / 60);
				m = m - h * 60
			}
			if (s < 10)
				s = "0" + s;
			if (m < 10)
				m = "0" + m;
			if (h < 10)
				h = "0" + h;
			this.elem.each(function() {
				this.innerHTML = h + ":" + m + ":" + s;
			});
		}

		Timer = function(elem_id, current_ms, callback, showType) {
			this.current_ms = current_ms;
			this.elem = jQuery('[id=' + elem_id + ']');
			this.count = 0;
			this.delay = false;
			this.callback = callback;
			this.timerID = 0;
			this.showType = showType;
			console.info(this.elem);
		}

		Timer.prototype.start = function() {
			var _self = this;
			this.timerID = setTimeout(function() {
				_self.start()
			}, 1000);

			if (this.elem.size() == 0)
				return;
			if (this.showType == "itemTimer") {
				this.showTime2();
			} else {
				this.showTime();
			}
			this.current_ms -= 1000;
			if (this.current_ms <= 0) {
				if (this.showType == "show2") {
					this.elem.each(function() {
						// this.innerHTML = "还剩:0小时0分钟0秒";
							this.innerHTML = "已结束";
						});
				} else {
					this.elem.innerHTML = "00:00:00";
				}
				if (this.callback && typeof this.callback == 'function') {
					try {
						this.callback();
					} catch (err) {
					}
				}
				return;
			}
		}

		Timer.prototype.showTime = function() {
			if (this.elem.size() == 0)
				return;

			var ms = this.current_ms;
			if (ms <= 0) {
				this.innerHTML = "已结束";
				this.clear();
				return;
			}
			var s = h = m = d = 0;
			s = Math.floor(ms / 1000);
			if (s >= 60) {
				m = Math.floor(s / 60);
				s = s - m * 60;
			}
			if (m >= 60) {
				h = Math.floor(m / 60);
				m = m - h * 60;
			}
			if (h >= 24) {
				d = Math.floor(h / 24);
				h = h - d * 24;
			}
			this.elem.each(function() {
				this.innerHTML = "剩余时间:" + (d > 0 ? d + "天" : "") + h + ":" + m
						+ ":" + s + "";
			});
		}

		Timer.prototype.showTime2 = function() {
			if (this.elem.size() == 0)
				return;
			var ms = this.current_ms;
			if (ms <= 0) {
				this.innerHTML = "已结束";
				this.clear();
				return;
			}
			var s = h = m = d = 0;
			s = Math.floor(ms / 1000);
			if (s >= 60) {
				m = Math.floor(s / 60);
				s = s - m * 60;
			}
			if (m >= 60) {
				h = Math.floor(m / 60);
				m = m - h * 60;
			}
			if (h >= 24) {
				d = Math.floor(h / 24);
				h = h - d * 24;
			}
			this.elem.each(function() {
				this.innerHTML = "(剩余:<b>" + (d > 0 ? d + "</b>天<b>" : "") + h
						+ "</b>小时<b>" + m + "</b>分<b>" + s + "</b>秒)";
			});
		}

		Timer.prototype.clear = function() {
			window.clearTimeout(this.timerID);
			this.current_ms = 0;
		}

		function startTimer(id, time, callback, showType) {
			if (time < 0)
				return null;
			var timer = new Timer(id, time, callback, showType);
			timer.start();
			return timer;
		}
		// 调用方式:
		var date = new Date();
		date.addDays(3);

		var remainTime = date.getTime() - new Date().getTime();
		console.info(remainTime);
		startTimer('show_time', remainTime, "", 'itemTimer');

		var DateUtil = new Object();

		// 回去生肖,传入参数必须是四位的年
		DateUtil.getAnimals = function(year) {
			var arr = ['猴', '鸡', '狗', '猪', '鼠', '牛', '虎', '兔', '龙', '蛇', '马',
					'羊']
			return /^\d{4}$/.test(year) ? arr[year] : null;
		};

		// 根据生日的月份和日期,计算星座。
		DateUtil.getConstellation = function(month, day) {
			var str = "魔羯水瓶双鱼牡羊金牛双子巨蟹狮子处女天秤天蝎射手魔羯";
			var arr = [20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22, 22];
			return str.substr(month * 2 - (day < arr[month - 1] ? 2 : 0), 2)
					+ "座";
		}

		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;
		};
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值