導航:首頁 > 編程語言 > js判斷當前系統時間

js判斷當前系統時間

發布時間:2022-10-31 10:24:38

js如何獲取當前系統時間

系統時間一般是值服務端時間,js獲取服務端時間的方法是直接用ajax獲取。
1、編寫顯示時間的頁面:
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Server date/time</title>
<script language="javascript" src="serverDate.js"></script>
</head>
<script language="javascript">
var localTime = new Date();
document.write("Local machine time is: " + localTime + "<br>");
document.write("Server time is: " + date);
</script>
<body>
</body>

2、ajax腳本獲取server的時間
var xmlHttp;
function srvTime(){
try {
//創建xmlHttp對象
xmlHttp = new XMLHttpRequest();
}
catch (err1) {
//ie瀏覽器
try {
xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
}
catch (err2) {
try {
xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
}
catch (eerr3) {
//ajax不支持
alert("AJAX not supported");
}
}
}
//打開xmlHttp請求
xmlHttp.open('HEAD',window.location.href.toString(),false);
//設置xmlHttp請求頭
xmlHttp.setRequestHeader("Content-Type", "text/html");
//發送請求
xmlHttp.send('');
// 獲取response中的Date參數
return xmlHttp.getResponseHeader("Date");
}
var st = srvTime(); //伺服器時間賦值給st變數
var date = new Date(st); //轉換js的date對象
// 輸出伺服器時間
document.write("伺服器時間: " + date);

❷ js 中怎麼獲取當前系統時間

var nowTime = new Date();nowTime.getDay();nowTime.getYear();nowTime.getMonth();nowTime.getHour();.....

❸ js 中怎麼獲取當前系統時間

系統時間一般是值服務端時間,js獲取服務端時間的方法是直接用獲取。

編寫顯示時間的頁面:

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Server date/time</title>

<script language="javascript" src="serverDate.js"></script>

</head>

<script language="javascript">

var localTime = new Date();

document.write("Local machine time is: " + localTime + "<br>");

document.write("Server time is: " + date);

</script>

<body>

</body>

ajax腳本獲取server的時間

var xmlHttp;

function srvTime(){

try {

//創建xmlHttp對象

xmlHttp = new XMLHttpRequest();

}

catch (err1) {

//ie瀏覽器

try {

xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');

}

catch (err2) {

try {

xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');

}

catch (eerr3) {

//ajax不支持

alert("AJAX not supported");

}

}

}

//打開xmlHttp請求

xmlHttp.open('HEAD',window.location.href.toString(),false);

//設置xmlHttp請求頭

xmlHttp.setRequestHeader("Content-Type", "text/html");

//發送請求

xmlHttp.send('');

// 獲取response中的Date參數

return xmlHttp.getResponseHeader("Date");

}

var st = srvTime(); //伺服器時間賦值給st變數

var date = new Date(st); //轉換js的date對象

// 輸出伺服器時間

document.write("伺服器時間: " + date);

拓展資料:

JavaScript一種直譯式腳本語言,是一種動態類型、弱類型、基於原型的語言,內置支持類型。它的解釋器被稱為JavaScript引擎,為瀏覽器的一部分,廣泛用於客戶端的腳本語言,最早是在HTML(標准通用標記語言下的一個應用)網頁上使用,用來給HTML網頁增加動態功能。

語句:JavaScript程序是由若干語句組成的,語句是編寫程序的指令。JavaScript提供了完整的基本編程語句,

它們是:賦值語句、switch選擇語句、while循環語句、for循環語句、for each循環語句、do...while循環語句、break循環中止語句、continue循環中斷語句、with語句、try…catch語句、if語句(if..else,if…else if…)。

❹ js獲取系統時間

剛寫的小例子,你可以參考下

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript">
function bodyLoad(){
var dateTime=new Date();
var hh=dateTime.getHours();
var mm=dateTime.getMinutes();
var ss=dateTime.getSeconds();

var yy=dateTime.getFullYear();
var MM=dateTime.getMonth()+1; //因為1月這個方法返回為0,所以加1
var dd=dateTime.getDate();

var week=dateTime.getDay();

var days=[ "日 ", "一 ", "二 ", "三 ", "四 ", "五 ", "六 ",]

document.getElementById("date").value=yy+"年"+MM+"月"+dd+"日 "+"星期"+days[week] ;
document.getElementById("time").value=hh+"時"+mm+"分"+ss+"秒";

setTimeout(bodyLoad,1000);
}
</script>
</head>

<body onload="bodyLoad()">

<input type="text" id="date" />
<br/>
<input type="text" id="time" />
</body>
</html>

❺ JS實現檢測當前系統時間是否可以點擊按鈕

document.querySelector("button").addEventListener("click", function() {
//上面哪一行是找一個button,給他添加一個click事件,你可以把button改成你的id
if((new Date()).getHours() >= 18) {
//判斷條件是創建出一個Date實例,用他的getHours()就可以獲取當前的系統時間的小時數
//執行你下午六點後的邏輯
} else {
return;
//這里是其他情況這個點擊事件的函數直接結束掉
}
})

PS:想要浪費時間嘗試一下setTimeout和setInterval也不是不可以,但是嘗試完你會發現你真的是在浪費時間。
========================================
更新:最簡單能用的例子,復制到記事本,另存為html文件
<!DOCTYPE html>
<html>
<body>
<button id="test">測試</button>
<script>
document.querySelector("#test").addEventListener("click", function() {
if((new Date()).getHours() >= 18) {
alert("18點之後的代碼在這里")
} else {
alert("不到18點的代碼在這里,如果不想執行別的,這里改成return")
}
});
</script>
</body>
</html>

❻ js獲取當前當前年月日時分秒,以及獲取年月日(無時分秒),詳情見補充!

1、新建一個HTML文件,命抄名為test.html。

❼ js判斷當前時間是不是當前周的

這個你需要獲取時間進行比較了哦:
var oDate = new Date(); //實例一個時間對象;
oDate.getFullYear(); //獲取系統的年;
oDate.getMonth()+1; //獲取系統月份,由於月份是從0開始計算,所以要加1
oDate.getDate(); // 獲取系統日,
oDate.getHours(); //獲取系統時,
oDate.getMinutes(); //分
oDate.getSeconds(); //秒

❽ JS如何獲取當前系統時間

系統時間一般是值服務端時間,js獲取服務端時間的方法是直接用ajax獲取。
1、編寫顯示時間的頁面:
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Server date/time</title>
<script language="javascript" src="serverDate.js"></script>
</head>
<script language="javascript">
var localTime = new Date();
document.write("Local machine time is: " + localTime + "<br>");
document.write("Server time is: " + date);
</script>
<body>
</body>

2、ajax腳本獲取server的時間
var xmlHttp;
function srvTime(){
try {
//創建xmlHttp對象
xmlHttp = new XMLHttpRequest();
}
catch (err1) {
//ie瀏覽器
try {
xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
}
catch (err2) {
try {
xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
}
catch (eerr3) {
//ajax不支持
alert("AJAX not supported");
}
}
}
//打開xmlHttp請求
xmlHttp.open('HEAD',window.location.href.toString(),false);
//設置xmlHttp請求頭
xmlHttp.setRequestHeader("Content-Type", "text/html");
//發送請求
xmlHttp.send('');
// 獲取response中的Date參數
return xmlHttp.getResponseHeader("Date");
}
var st = srvTime(); //伺服器時間賦值給st變數
var date = new Date(st); //轉換js的date對象
// 輸出伺服器時間
document.write("伺服器時間: " + date);

❾ jquery如何獲取當前系統時間js獲取系統時間

給獲取時間定義的一個獲取時間方法,在該方法內去獲取
function getTime(){
var nowDate=new Date();
var year=nowDate.getFullYear();
var month=nowDate.getMonth()+1<10?"0"+(nowDate.getMonth()+1):nowDate.getMonth()+1;
var date=nowDate.getDate()<10?"0"+nowDate.getDate():nowDate.getDate();
var hour=nowDate.getHours()<10?"0"+nowDate.getHours():nowDate.getHours();
var minute=nowDate.getMinutes()<10?"0"+nowDate.getMinutes():nowDate.getMinutes();
var second=nowDate.getSeconds()<10?"0"+nowDate.getSeconds():nowDate.getSeconds();
return year+month+date+hour+minute+second;
}

❿ js如何獲得系統時間年月日時分秒

javascript 自帶有個對象(構造函數),Date().下面是代碼:

varoDate=newDate();//實例一個時間對象;
oDate.getFullYear();//獲取系統的年;
oDate.getMonth()+1;//獲取系統月份,由於月份是從0開始計算,所以要加1
oDate.getDate();//獲取系統日,
oDate.getHours();//獲取系統時,
oDate.getMinutes();//分
oDate.getSeconds();//秒
閱讀全文

與js判斷當前系統時間相關的資料

熱點內容
蘋果11網路鎖怎麼解 瀏覽:276
城市大數據分析 瀏覽:291
法國電影一個男的兩個女的 瀏覽:212
妹妹和姐姐電影 瀏覽:999
linux串口後台運行 瀏覽:113
維語紅色電影 瀏覽:213
錢勝成都大數據 瀏覽:786
主角穿越多個現實位面的小說 瀏覽:578
java查詢access資料庫 瀏覽:438
一顆種子長上天的電影 瀏覽:846
手機壓縮文件圖標和別人發的不同 瀏覽:226
徹底刪除cad2016殘留文件 瀏覽:255
大數據時代語文教學設計 瀏覽:277
530電影電視劇在線免費觀看 瀏覽:869
好看的愛情台灣電影有哪些 瀏覽:834
智慧門工具 瀏覽:665
姜恩惠善良的愛 瀏覽:20
免費在線觀看網站網址 瀏覽:565
鋼琴女老師韓國 瀏覽:858
文件保存路徑能修改嘛 瀏覽:518

友情鏈接