Home >Web Front-end >JS Tutorial >How does JavaScript calculate time difference (introducing external font files)?
This chapter will show you how to calculate the time difference (introducing external font files) using JavaScript? It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
JavaScript Date() object:
new Date(): time object, which will use the current time as its initial value;
SetFullYear(): used to set the month, can have three parameters, setFullYear(year,month,day);
setHours(): set the hour of the specified time, can have four parameters, setHours( hour, min, sec, millisec);
GetDate(): Returns a certain day of the month;
getMonth(): Returns the number representing the month;
getFullYear() : Returns a 4-digit number representing the year;
GetTime() : Returns the number of milliseconds from January 1, 1970;
setFullYear() : Sets the year;
SetDate(): Set the day;
Code example:
css code:
@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; }
HTML code:
<div class="contain"></div>
js code:
<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>
No need to install it locally, directly reference the external font file
@font-face{ font-family: "Digital-7 Mono"; src: url('Digital-7Mono.TTF'); }
URL is the path of the file
The above is the detailed content of How does JavaScript calculate time difference (introducing external font files)?. For more information, please follow other related articles on the PHP Chinese website!