Home > Web Front-end > JS Tutorial > body text

How does JavaScript calculate time difference (introducing external font files)?

青灯夜游
Release: 2018-09-13 17:14:41
Original
1722 people have browsed it

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;
        }
Copy after login

HTML code:

<div class="contain"></div>
Copy after login

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=&#39;years&#39;>" + years + "</span> year ":"")
            result += (months >= 0 ? "<span class=&#39;months&#39;>" + months + "</span> month ":"");
            result += "<span class=&#39;days&#39;>" + days + "</span> day ";
            result += "<span class=&#39;hours&#39;>" + hours + "</span> hr "
            result += "<span class=&#39;minutes&#39;>" + minutes + "</span> min "
            result += "<span class=&#39;seconds&#39;>" + seconds + "</span> sec"

        $(".contain").html(result);
    }
    Time(time,displayMode);
    
    setInterval(function(){
        Time(time,displayMode);
    },1000)
</script>
Copy after login

No need to install it locally, directly reference the external font file

@font-face{
            font-family: "Digital-7 Mono";
            src: url(&#39;Digital-7Mono.TTF&#39;);
        }
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!