一: 使用js获取网络时间
1 var xhr = new XMLHttpRequest(); 2 if( !xhr ){ 3 xhr = new ActiveXObject("Microsoft.XMLHTTP"); 4 } 5 xhr.open("HEAD",location.href,true); 6 xhr.onreadystatechange=function(){ 7 if( xhr.readyState == 4 && xhr.status == 200 ){ 8 alert(dateTimeFormate(xhr.getResponseHeader("Date"))); 9 } 10 } 11 xhr.send(null);
日期格式化根据代码
1 function dateTimeFormate(date){ 2 if(!date){ 3 return 4 }else{ 5 var d = new Date(date); 6 var year = d.getFullYear(); 7 var month = ('0' + (d.getMonth() + 1)).slice(-2); 8 var day = ('0' + (d.getDate())).slice(-2); 9 var hour = ('0' + (d.getHours())).slice(-2); 10 var minutes = ('0' + (d.getMinutes())).slice(-2); 11 var seconds = ('0' + (d.getSeconds())).slice(-2); 12 return year + "-" + month + "-" + day + " " + hour + ":" + minutes + ":" + seconds; 13 } 14 }
二: 使用jquery获取网络时间
1 dateTimeFormate($.ajax({async:false}).getResponseHeader("Date"))