本文目錄一覽:
如何用javascript實現一個時鐘?
function init(){
clock();
setInterval(clock,1000);
}
function clock(){
var now = new Date();
var ctx = document.getElementById(‘canvas’).getContext(‘2d’);
ctx.save();
ctx.clearRect(0,0,150,150);
ctx.translate(75,75);
ctx.scale(0.4,0.4);
ctx.rotate(-Math.PI/2);
ctx.strokeStyle = “black”;
ctx.fillStyle = “white”;
ctx.lineWidth = 8;
ctx.lineCap = “round”;
// Hour marks
ctx.save();
for (var i=0;i12;i++){
ctx.beginPath();
ctx.rotate(Math.PI/6);
ctx.moveTo(100,0);
ctx.lineTo(120,0);
ctx.stroke();
}
ctx.restore();
// Minute marks
ctx.save();
ctx.lineWidth = 5;
for (i=0;i60;i++){
if (i%5!=0) {
ctx.beginPath();
ctx.moveTo(117,0);
ctx.lineTo(120,0);
ctx.stroke();
}
ctx.rotate(Math.PI/30);
}
ctx.restore();
var sec = now.getSeconds();
var min = now.getMinutes();
var hr = now.getHours();
hr = hr=12 ? hr-12 : hr;
ctx.fillStyle = “black”;
// write Hours
ctx.save();
ctx.rotate( hr*(Math.PI/6) + (Math.PI/360)*min + (Math.PI/21600)*sec )
ctx.lineWidth = 14;
ctx.beginPath();
ctx.moveTo(-20,0);
ctx.lineTo(80,0);
ctx.stroke();
ctx.restore();
// write Minutes
ctx.save();
ctx.rotate( (Math.PI/30)*min + (Math.PI/1800)*sec )
ctx.lineWidth = 10;
ctx.beginPath();
ctx.moveTo(-28,0);
ctx.lineTo(112,0);
ctx.stroke();
ctx.restore();
// Write seconds
ctx.save();
ctx.rotate(sec * Math.PI/30);
ctx.strokeStyle = “#D40000”;
ctx.fillStyle = “#D40000”;
ctx.lineWidth = 6;
ctx.beginPath();
ctx.moveTo(-30,0);
ctx.lineTo(83,0);
ctx.stroke();
ctx.beginPath();
ctx.arc(0,0,10,0,Math.PI*2,true);
ctx.fill();
ctx.beginPath();
ctx.arc(95,0,10,0,Math.PI*2,true);
ctx.stroke();
ctx.fillStyle = “#555”;
ctx.arc(0,0,3,0,Math.PI*2,true);
ctx.fill();
ctx.restore();
ctx.beginPath();
ctx.lineWidth = 14;
ctx.strokeStyle = ‘#325FA2’;
ctx.arc(0,0,142,0,Math.PI*2,true);
ctx.stroke();
ctx.restore();
}
關於網頁調用系統時間JS代碼
!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01//EN” “”
html
head
meta http-equiv=”Content-Type” content=”text/html; charset=gbk”
titleUntitled Document/title
script
//————-可以把以下內容寫進外部js文件中————
window.onload=getTime; //網頁加載完就調用getTime()方法
function getTime(){
var today=new Date(); //獲取當前系統時間
var hours=today.getHours(); //獲取小時
var minutes=today.getMinutes(); //獲取分
var seconds=today.getSeconds(); //獲取秒
if(minutes10){ //分小於10就在前面補0
minutes=”0″+minutes;
}
if(seconds10){ //秒小於10就在前面補0
seconds=”0″+seconds;
}
//把時分秒拼起來得到時間
var time =hours+”:”+minutes+”:”+seconds;
//把時間顯示在div上,您自己可以放在網頁任何位置,反正時間就是time
document.getElementById(“div”).innerHTML=time;
//每隔一秒更新一次時間
setTimeout(“getTime()”,1000);
}
//————-可以把以上內容寫進外部js文件中————
/script
/head
body
div id=”div”/div
/body
/html
代碼已經貼上,希望對您有幫助
JS中獲取當前時間的代碼是什麼?
Date 對象
啟用基本存儲器並取得日期和時間。
dateObj = new Date()
dateObj = new Date(dateVal)
dateObj = new Date(year, month, date[, hours[, minutes[, seconds[,ms]]]])
參數
dateObj
必選項。要賦值為 Date 對象的變量名。
dateVal
必選項。如果是數字值,dateVal 表示指定日期與 1970 年 1 月 1 日午夜間全球標準時間 的毫秒數。如果是字符串,則 dateVal 按照 parse 方法中的規則進行解析。dateVal 參數也可以是從某些 ActiveX(R) 對象返回的 VT_DATE 值。
year
必選項。完整的年份,比如,1976(而不是 76)。
month
必選項。表示的月份,是從 0 到 11 之間的整數( 1 月至 12 月)。
date
必選項。表示日期,是從 1 到 31 之間的整數。
hours
可選項。 如果提供了 minutes 則必須給出。表示小時,是從 0 到 23 的整數(午夜到 11pm)。
minutes
可選項。 如果提供了 seconds 則必須給出。表示分鐘,是從 0 到 59 的整數。
seconds
可選項。 如果提供了 milliseconds 則必須給出。表示秒鐘,是從 0 到 59 的整數。
ms
可選項。 表示毫秒,是從 0 到 999 的整數。
說明
Date 對象保存以毫秒為單位表示特定時間段。如果某個參數的值大於其範圍或為負數,則存儲的其他值將做相應的調整。例如,如果指定 150 秒,JScript 將該數字重新定義為 2 分 30 秒。
如果數字為 NaN,則表示該對象不代表特定的時間段。如果未向 Date 對象傳遞參數,它將被初始化為當前時間 (UTC)。在能夠使用該對象前必須為其賦值。
Date 對象能夠表示的日期範圍約等於 1970 年 1 月 1 日前後各 285,616 年。
Date 對象具有兩個不創建 Date 對象就可以調用的靜態方法。它們是 parse 和 UTC。
錯誤
下面的示例演示了 Date 對象的用法。
function DateDemo(){
var d, s = “Today’s date is: “; // 聲明變量。
d = new Date(); // 創建 Date 對象。
s += (d.getMonth() + 1) + “/”; // 獲取月份。
s += d.getDate() + “/”; // 獲取日。
s += d.getYear(); // 獲取年份。
return(s); // 返回日期。
}
js時間代碼
!DOCTYPE html
html lang=”en”
head
meta charset=”UTF-8″
titledateutil-js時間舉例/title
!– script src=””/script–
script src=””/script
/head
body
script type=”text/javascript”
console.log(getdate_WMdy_En());//Thurs.Sept.2, 2020
console.log(getdate_yMdhms_T());//2020-9-2 21:41:7
console.log(getdate_WyMdhms_C());//星期四 2020年9月2日 21時38分33秒
/script
/body
/html
js獲得當前日期和時間的代碼是什麼?
var
myDate
=
new
Date();
myDate.toLocaleDateString();可以獲取當前日期
myDate.toLocaleTimeString();
可以獲取當前時間
擴展:
myDate.getYear();
//獲取當前年份(2位)
myDate.getFullYear();
//獲取完整的年份(4位,1970-????)
myDate.getMonth();
//獲取當前月份(0-11,0代表1月)
myDate.getDate();
//獲取當前日(1-31)
myDate.getDay();
//獲取當前星期X(0-6,0代表星期天)
myDate.getTime();
//獲取當前時間(從1970.1.1開始的毫秒數)
myDate.getHours();
//獲取當前小時數(0-23)
myDate.getMinutes();
//獲取當前分鐘數(0-59)
myDate.getSeconds();
//獲取當前秒數(0-59)
myDate.getMilliseconds();
//獲取當前毫秒數(0-999)
myDate.toLocaleString(
);
//獲取日期與時間
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/247272.html