script language="JavaScript"
成都創(chuàng)新互聯(lián)主要從事成都網(wǎng)站設(shè)計、成都網(wǎng)站制作、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)右玉,10年網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18982081108
function showTime(){
var div=document.getElementById("timeDiv");
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();
div.innerHTML=year+"-"+format(month)+"-"+format(day)+" "+format(hour)+":"+format(minute)+":"+format(second);
}
function format(i){
return i10?"0"+i:i;
}
/script
html
body onload="setInterval(showTime,1000);"/body
/html
div id="timeDiv"div
public?static?void?main(String[]?args)?throws?ParseException?{
//當(dāng)前時間
Date?now=new?Date();
SimpleDateFormat?sdf=new?SimpleDateFormat("yyyy-MM-dd?HH:mm:ss");
//今年3月5號10點15分20轉(zhuǎn)成Date類型
Date?old=sdf.parse("2016-03-05?10:15:20");
//計算時間差
long?diff=now.getTime()-old.getTime();
long?day=diff/(24*60*60*1000);
long?hour=(diff/(60*60*1000)-day*24);
long?min=((diff/(60*1000))-day*24*60-hour*60);
long?s=(diff/1000-day*24*60*60-hour*60*60-min*60);
System.out.println(""+day+"天"+hour+"小時"+min+"分"+s+"秒");
}
start用來記錄開始時間,end用來記錄程序結(jié)束時間。那個for循環(huán)就是模擬你load頁面的時間
明白了么?
long start = System.currentTimeMillis();
for(int i = 1; i 10000000; i++){
for(int j = 1; j 100; j++){
}
}
long end = System.currentTimeMillis();
System.out.println("Start time for pgm: " + start);
System.out.println("End time for pgm: " + end);
System.out.println("Total " + (end - start) + " seconds to run the program above");
----------
Start time for pgm: 1291953801885
End time for pgm: 1291953803994
Total 2109 seconds to run the program above
html
head
title當(dāng)前時間/title
/head
!--將以下代碼加入HTML的Body/Body之間--
script language=JavaScript
var timerID = null;
var timerRunning = false;
function stopclock (){
if(timerRunning)
clearTimeout(timerID);
timerRunning = false;}
function startclock () {
stopclock();
showtime();}
function showtime () {
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds()
var timeValue = "" +((hours = 12) ? "下午 " : "上午 " )
timeValue += ((hours 12) ? hours -12 :hours)
timeValue += ((minutes 10) ? ":0" : ":") + minutes
timeValue += ((seconds 10) ? ":0" : ":") + seconds
document.clock.thetime.value = timeValue;
timerID = setTimeout("showtime()",1000);
timerRunning = true;}
/SCRIPT
body onload=startclock()
form name=clock
input name=thetime style="font-size: 9pt;color:#000000;border:0px none; " size=12
/form
/body/html
下面這個javascript是每秒顯示一次時間,你只需要把下面的showTime()函數(shù)里面程序一修改就可以得到你想要的結(jié)果了
setInterval("showTime()", 1000);
function showTime()
{
var today = new Date();
alert("The time is: " + today.toString());
} 答案補充 setInterval("showTime()", 1000);
function showTime()
{
//你把這個時間變成你想要的就可以了啊,然后每過一秒時間就加一秒
var today = new Date(2008,11,30,11,20,45);
alert("The time is: " + today.toString());
} 答案補充 獲取日期的時間方法
getYear(): 返回年數(shù)
getMonth():返回當(dāng)月號數(shù)
getDate(): 返回當(dāng)日號數(shù)
getDay():返回星期幾
getHours():返回小時數(shù)
getMintes(:返回分鐘數(shù)
getSeconds():返回秒數(shù)
getTime() : 返回毫秒數(shù)
設(shè)置日期和時間:
setYear();設(shè)置年
setDate():設(shè)置當(dāng)月號數(shù)
setMonth():設(shè)置當(dāng)月份數(shù)
setHours():設(shè)置小時數(shù)
setMintes():設(shè)置分鐘數(shù)
setSeconds():設(shè)置秒數(shù)
setTime ():設(shè)置毫秒數(shù)
例子:
var d = new Date("2008/11/30");
d.setMonth(d.getMonth() + 1 + 1);//加一個月,同理,可以加一天:getDate()+1,加一年:getYear()+1