本章帶給大家用JavaScript如何計算時間差(引入外部字體檔案)?有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。
JavaScript Date() 物件:
new Date() :時間對象,會將目前時間當作其初始值;
setFullYear() :用於設定月份,可有三個參數,setFullYear(year,month,day);
setHours() :設定指定時間的小時,可有四個參數,setHours( hour,min,sec,millisec);
getDate() :返回月份的某一天;
getMonth() :傳回表示月份的數字;
getFullYear() :傳回表示月份的數字;
getFullYear() :傳回一個表示年份的4 位數;
getTime() :傳回距離1970 年1 月1 日之間的毫秒數;
setFullYear() :設定年份;
setDate() :設定日;#程式碼實例:
#################################################################################################################### #####css程式碼:######
@font-face{ font-family: "Digital-7 Mono"; src: url('Digital-7Mono.TTF'); } div{ width: 500px; } .contain{ text-align: right; font-size: 18px; margin-top: 10px; margin-bottom: 10px; } .contain span{ font-family: "Digital-7 Mono"; font-size: 36px; color: #555; padding-left: 10px; }
<div class="contain"></div>
<script src="jquery.min.js"></script> <script type="text/javascript"> var displayMode = 1; var time; $(".contain").click(function(){ displayMode *= -1; Time(time, displayMode); }); // 返回不同月份不同天数的方法 function getDaysInMonth(month) { var data = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; return data[month]; } // 设置开始时间 function start(year,month,day,hour,min,sec,mill){ var starttime =new Date(); starttime.setFullYear(year,month-1,day); starttime.setHours(hour,min,sec,mill); time =starttime; return time; } start(2015,2,20,21,0,0,0); function Time(date,mode){ var years = NaN; var months = NaN; var current =new Date(); var seconds =(Date.parse(current) - Date.parse(date)) /1000; //获取时间差的秒数 var days =Math.floor(seconds / (3600 * 24)); //总天数 seconds = seconds % (3600 * 24); //总秒数 % 一天的秒数 下面的同理 var hours =Math.floor(seconds / 3600); seconds = seconds % 3600; var minutes = Math.floor(seconds / 60); seconds = seconds % 60; // 判断假如时分秒小时10的话 前面加0 (seconds <10)? seconds ="0"+seconds:seconds=seconds; (hours <10)? hours ="0"+hours:hours=hours; (minutes <10)? minutes ="0"+minutes:minutes=minutes; if (mode == 1) { days = current.getDate() - date.getDate(); //当前日 - 开始日 if (days < 0) { days += getDaysInMonth(current.getMonth()); current.setDate(current.getDate() -1); } months = current.getMonth() - date.getMonth(); if (months < 0) { months += 12; current.setFullYear(current.getFullYear() - 1); } years = current.getFullYear() - date.getFullYear(); } else { days = Math.floor((current.getTime() - date.getTime()) / (1000 * 3600 * 24)); } var result = (years > 0 ? "<span class='years'>" + years + "</span> year ":"") result += (months >= 0 ? "<span class='months'>" + months + "</span> month ":""); result += "<span class='days'>" + days + "</span> day "; result += "<span class='hours'>" + hours + "</span> hr " result += "<span class='minutes'>" + minutes + "</span> min " result += "<span class='seconds'>" + seconds + "</span> sec" $(".contain").html(result); } Time(time,displayMode); setInterval(function(){ Time(time,displayMode); },1000) </script>
@font-face{ font-family: "Digital-7 Mono"; src: url('Digital-7Mono.TTF'); }
以上是JavaScript如何計算時間差(引入外部字體檔案)?的詳細內容。更多資訊請關注PHP中文網其他相關文章!