js 獲取服務器當前時間


 //獲取服務器時間

function getNowDate(){
var xhr = null;
if(window.XMLHttpRequest){
xhr = new window.XMLHttpRequest();
}else{
xhr = new ActiveObject("Microsoft")
}

xhr.open("GET","/",false)
xhr.send(null);
var date = xhr.getResponseHeader("Date");
date = new Date(date);
return date;
}

 

//獲取當前時間

function getNowFormatDate() {

var date = new Date();
var char1 = "-";
var char2 = ":";
var month = date.getMonth() + 1;
var strDate = date.getDate();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
var currentTime = date.getFullYear() + char1 + month + char1 + strDate
+ " " + date.getHours() + char2 + date.getMinutes()
+ char2 + date.getSeconds();
return currentTime;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM