給獲取時(shí)間定義的一個(gè)獲取時(shí)間方法,在該方法內(nèi)去獲取
創(chuàng)新互聯(lián)公司主要從事成都網(wǎng)站建設(shè)、做網(wǎng)站、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)新賓,10年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):028-86922220
function getTime(){
var nowDate=new Date();
var year=nowDate.getFullYear();
var month=nowDate.getMonth()+110?"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;
}
jquery獲取時(shí)間戳用event的timeStamp方法獲取。
例如:
顯示出當(dāng)對按鈕元素的點(diǎn)擊事件發(fā)生時(shí)的時(shí)間戳:
html
head
script type="text/javascript" src="/jquery/jquery.js"/script
script type="text/javascript"
$(document).ready(function(){
$("button").click(function(e){
$("span").text(e.timeStamp);
});
});
/script
/head
body
p對下面這個(gè)按鈕的點(diǎn)擊事件發(fā)生在 1970 年 1 月 1 日之后 spanunknown/span 毫秒。/p
buttonClick me/button
/body
/html
運(yùn)行結(jié)果:
jQuery是JavaScript的框架,jQuery不是什么都可以實(shí)現(xiàn)的。獲取系統(tǒng)時(shí)間需要用原生JavaScript。
!DOCTYPE?html
html
head
meta?charset="UTF-8"?/
titleHTML5學(xué)堂/title
/head
body
div?class="h5course"學(xué)習(xí)HTML5,到HTML5學(xué)堂/div
input?class="h5course-com"?id="h5courseCom"?type="checkbox"?/?
label?for="h5courseCom"HTML5學(xué)堂,一個(gè)神奇的網(wǎng)站/label
script?type="text/javascript"
var?h5course_com?=?new?Date();
alert('HTML5學(xué)堂:'?+?h5course_com.getHours()?+?':'?+?h5course_com.getMinutes()?+?':'?+?h5course_com.getSeconds());?
/script
/body
/html
jquery或者javascript獲取服務(wù)器的特定時(shí)間:
通過執(zhí)行ajax方式返回服務(wù)器時(shí)間
*/
$(document).ready(function (){
var url="/ajax/comm.shtml?action=getTime";
jQuery.ajax(url,{
data: "",
dataType: "json",
cache: false,
success: function(req){
callbackTime(req);
},
error: function(msg){
alert("ERROR:請刷新頁面!");
}
});
$("#but1").click(function(){jqueryTest();});
});
var srv_nowtime;
function callbackTime(req){
var timeArr = req.split("");
srv_nowtime = new Date(timeArr[0]).getTime();
showTime();
window.setInterval(showTime, 1000);
}
function showTime(){
srv_nowtime += 1000;
var nowtime = new Date(srv_nowtime);
var ymd = nowtime.getFullYear() + ""+ (nowtime.getMonth()+1) +""+ (nowtime.getDate()10?"0"+nowtime.getDate():nowtime.getDate());
}
執(zhí)行之后就會顯示服務(wù)器時(shí)間