在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML5  Python  HTML/ 想用python寫一個實(shí)時(shí)計(jì)時(shí)程序(或用html寫),怎么實(shí)現(xiàn)?

想用python寫一個實(shí)時(shí)計(jì)時(shí)程序(或用html寫),怎么實(shí)現(xiàn)?

如果一運(yùn)行這段代碼就會打印出時(shí)實(shí)變化的時(shí)間
比如:已運(yùn)行0天0小時(shí)0分鐘15秒
每過一秒會自動刷新打印結(jié)果。

回答
編輯回答
故林
  1. print()如何不換行在同一行刷新顯示
  2. 獲取當(dāng)前系統(tǒng)時(shí)間(通過當(dāng)前系統(tǒng)時(shí)間減去程序進(jìn)入時(shí)的時(shí)間來計(jì)算已運(yùn)行時(shí)間會比較準(zhǔn)確,前提是期間你不要修改系統(tǒng)時(shí)間)
  3. 時(shí)間的格式化問題
2017年11月6日 21:34
編輯回答
陌如玉

console的寫法。

import sys
from datetime import datetime
from time import sleep

start_time = datetime.now()

while True:
    sleep(1)
    delta = datetime.now() - start_time
    message = 'Program running time: {}'.format(str(delta).split('.')[0])
    sys.stdout.write('\r' + message)
    sys.stdout.flush()
2018年3月22日 02:27
編輯回答
蔚藍(lán)色

print函數(shù)結(jié)尾默認(rèn)是換行,改成\r代表回車print('xxx', end='\r')

import time


def second_to_time(seconds):
    minute, second = divmod(seconds, 60)
    hour, minute = divmod(minute, 60)
    day, hour = divmod(hour, 24)
    return day, hour, minute, second


def str_time(s):
    return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(s))


def main():
    ts = time.time()
    print('程序開始時(shí)間: ', str_time(ts))
    try:
        while True:
            time.sleep(1)
            tn = time.time()
            total_second = int(tn - ts)
            day, hour, minute, second = second_to_time(total_second)
            current_time = str_time(tn)
            echo_info = '當(dāng)前時(shí)間: {t}, 已運(yùn)行{day}天{hour}小時(shí){minute}分鐘{second}秒'\
                .format(
                    t=current_time,
                    day=day,
                    hour=hour,
                    minute=minute,
                    second=second
                )

            print(echo_info, end='\r')
    except KeyboardInterrupt:
        current_time = str_time(time.time())
        print('\n程序結(jié)束時(shí)間: {}'.format(current_time))

if __name__ == '__main__':
    main()
2017年3月26日 05:02
編輯回答
舊螢火

你要的html

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>hello world</title>
</head>

<body>
    <div id="time"></div>
    <input type="button" id="start" value="開始">
    <input type="button" id="stop" value="暫停">
    <input type="button" id="reset" value="重置">

    <script>
        var timer = (function () {
            var start, temp;

            function run() {
                start = start || +new Date();
                if (temp) {
                    start += (new Date() - temp);
                    temp = 0;
                }
            }

            function stop() {
                if (start && !temp) {
                    temp = +new Date();
                }
            }

            function reset() {
                start = 0;
                temp = 0;
            }

            function getTime() {
                return start ? (temp ? temp : new Date()) - start : 0;
            }

            function getText() {
                var now = getTime() / 1000 >> 0;
                var h, m, s;
                s = now % 60;
                now = now / 60 >> 0;
                m = now % 60;
                now = now / 60 >> 0;
                h = now % 24;
                now = now / 24 >> 0;
                return now + ' 天 ' + h + ' 時(shí) ' + m + ' 分 ' + s + ' 秒 ';
            }

            return {
                start: run,
                stop: stop,
                reset: reset,
                getTime: getTime,
                getText: getText
            }
        })();

        function getId(id) {
            return document.getElementById(id);
        }

        getId('start').onclick = timer.start;
        getId('stop').onclick = timer.stop;
        getId('reset').onclick = timer.reset;

        setInterval(function () {
            getId('time').innerText = timer.getText();
        }, 100);
    </script>
</body>

</html>
2017年3月12日 16:10