1. 时间相关模块
1.1.1. 获得服务器时间
function get_server_time(){
var xmlHttp = false;
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e2) {
xmlHttp = false;
}
}
if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
xmlHttp = new XMLHttpRequest();
}
xmlHttp.open("GET", window.location.href.toString(), false);
xmlHttp.setRequestHeader("If-None-Match", "bytes=-1");
xmlHttp.setRequestHeader("Cache-Control","no-cache");
xmlHttp.send(null);
return new Date(xmlHttp.getResponseHeader("Date"));
}
1.1.2. 时间格式转化,两者时间相比较
Date.prototype.Format = function(fmt){
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(fmt))
fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
for(var k in o)
if(new RegExp("("+ k +")").test(fmt))
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
return fmt;
}
$(function(){
function dateCompare(dateStart,dateEnd){
dateStart = dateStart.replace(/\-/gi,"/");
dateEnd = dateEnd.replace(/\-/gi,"/");
var time1 = new Date(dateStart).getTime();
var time2 = new Date(dateEnd).getTime();
if(time2 > time1){
return true;
}
return false;
}
运用场景如:
if(dateCompare(new Date($.cookie("dataTime")).Format("yyyy-MM-dd"),new Date().Format("yyyy-MM-dd"))){
}
})
1.1.3. 把时间转化为准备时间
function get_unix_time(dateStr){
var newstr = dateStr.replace(/-/g,'/');
var date = new Date(newstr);
return date;
}
1.1.4. 计算已经开团时间
var indate = $("#start_date").val();
var todate = get_server_time();
var olddate = get_unix_time(indate);
var timepass = 0;
function showTime(){
var timelong = (todate - olddate) + timepass;
var h = parseInt(timelong/3600000,10);
var m = parseInt(timelong%3600000/60000,10);
var s = parseInt(timelong%60000/1000);
document.getElementById("datetime").innerHTML = (h < 10 ? "0" + h : h) + ":" + (m < 10 ? "0" + m : m) + ":" + (s < 10 ? "0" + s : s);
setTimeout(function(){
timepass += 1000;
showTime();
},1000);
}
showTime();