var myDate = new Date();
var year = myDate.getFullYear();
var month = myDate.getMonth() + 1;
var date = myDate.getDate();
var dateArr = ["日","一",'二','三','四','五','六'];
var day = myDate.getDay();
var hours = myDate.getHours();
var minutes = formatTime(myDate.getMinutes());
var seconds = formatTime(myDate.getSeconds());
var systemTime = document.getElementById("time");
alert( " " + year + "年專" + month +"月" + date + "日" + " 星期屬" + dateArr[day] + " " + hours + ":" + minutes + ":" + seconds);
② 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);
③ JavaScript 網頁獲取系統時間
時間襲
var date = new Date(); //我們把時間函數拿過來用一下 Date()
date.getFullYear(); //獲取年份
date.getMonth() + 1;//獲取月份
date.getDate(); //獲取日期
date.getHours(); //獲取小時
date.getMinutes(); //獲取分鍾
date.getSeconds(); //獲取秒數
date.getDay(); //獲取星期幾
④ JavaScript中如何獲得系統當前時間
function time(){
抄var now=new Date();
var month=now.getMonth()+1;
var day=now.getDate();
var hour=now.getHours();
var min=now.getMinutes();
var s=now.getSeconds();
setTimeout("time()",1000);
document.mytime.nowtime.value="當前襲時間:"month+"月"+day+"日"+hour+"時"+min+"分"+s+"秒"; setTimeout("time()",1000);
}
getMonth()值從0開始到11,所以實際值要加1
getDate()是取號數
getDay()是取星期數
⑤ javascript 如何修改系統時間
javascript是基於客戶端的腳本語言,它主要用於網頁的一些特效及一些事件的處理,它是沒辦法修改系統時間的,只能獲取系統時間。
⑥ JavaScript怎麼獲取系統時間
//獲取當前日期yyyy-MM-dd
function getNowFormatDate() {
var date = new Date();
var seperator1 = "-";
//var seperator2 = ":";
var year = date.getFullYear();
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 currentdate = year + seperator1 + month + seperator1 + strDate;
return currentdate;
}
//獲取當前日期yyyy-MM-dd HH:mm:ss
function getNowFormatDate1() {
var date = new Date();
var seperator1 = "-";
var seperator2 = ":";
var year = date.getFullYear();
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 currentdate = year + seperator1 + month + seperator1 + strDate
+ " " + date.getHours() + seperator2 + date.getMinutes()
+ seperator2 + date.getSeconds();
return currentdate;
}
⑦ js如何獲得系統時間年月日時分秒
javascript 自帶有個對象(構造函數),Date().下面是代碼:
varoDate=newDate();//實例一個時間對象;
oDate.getFullYear();//獲取系統的年;
oDate.getMonth()+1;//獲取系統月份,由於月份是從0開始計算,所以要加1
oDate.getDate();//獲取系統日,
oDate.getHours();//獲取系統時,
oDate.getMinutes();//分
oDate.getSeconds();//秒
⑧ 如何在javascript獲取當前系統時間
var day=now.getDay(); //getDay好像是獲取星期幾..var day=now.getDate();//這個是獲取幾號的.var month=now.getMonth()+1; //月份需要+1 是從0-11 表示內1到12月的.現在容1月,所以是0
⑨ javascript如何獲取系統時間
/*
timeStyle :日期時間格式
*/
function getDateTime(timeStyle) {
var date = new Date();
var year = date.getFullYear(); //年
var month = date.getMonth() + 1; //月
var day = date.getDate(); //日
var hour = date.getHours(); //小時
var minute = date.getMinutes(); //分鍾
var second = date.getSeconds(); //秒鍾
//月份小於10時顯示為'0X'
if (month < 10) {
month = "0" + month;
}
//天數小於10時顯示為'0X'
if (day < 10) {
day = "0" + day;
}
//小時小於10時顯示為'0X'
if (hour < 10) {
hour = "0" + hour;
}
//分鍾小於10時顯示為'0X'
if (minute < 10) {
minute = "0" + minute;
}
//秒鍾小於10時顯示為'0X'
if (second < 10) {
second = "0" + second;
}
var colon = ":";
var dash = "-";
var result = "";
if (timeStyle == "yyyy-MM-dd HH:mm") {
result = year + dash + month + dash + day + " "
+ hour + colon + minute;
}
else if (timeStyle == "yyyy-MM-dd HH:mm:ss") {
result = year + dash + month + dash + day + " "
+ hour + colon + minute + colon + second;
}
else {
result = year + dash + month + dash + day;
}
result += " ";
if(hour<=5){
result += "凌晨好!";
}else if(hour<=8){
result += "早上好!";
}else if(hour<=12){
result += "上午好!";
}else if(hour<=13){
result += "中午好!";
}else if(hour<=18){
result += "下午好!";
}else if(hour<=22){
result += "晚上好!";
}else if(hour<=24){
result += "深夜了,晚安!";
}
return result;
}
function runTime(){
showTime.innerHTML=getDateTime('yyyy-MM-dd HH:mm:ss');
setTimeout("runTime()",1000);
}
<font id="showTime">這里顯示時間!</font>
⑩ 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…)。