zoukankan      html  css  js  c++  java
  • HTML 中获取现在时间,实时时间获取

    JavaScript   Date 对象

                               Date 对象用于处理日期与实际。

    创建 Date 对象:

                     var now  =  new Date().

    方法描述
    getDate() 从 Date 对象返回一个月中的某一天 (1 ~ 31)。
    getDay() 从 Date 对象返回一周中的某一天 (0 ~ 6)。
    getFullYear() 从 Date 对象以四位数字返回年份
    getHours() 返回 Date 对象的小时 (0 ~ 23)。
    getMilliseconds() 返回 Date 对象的毫秒(0 ~ 999)。
    getMinutes() 返回 Date 对象的分钟 (0 ~ 59)。
    getMonth() 从 Date 对象返回月份 (0 ~ 11)。
    getSeconds() 返回 Date 对象的秒数 (0 ~ 59)。
       
    setDate() 设置 Date 对象中月的某一天 (1 ~ 31)。
    setFullYear() 设置 Date 对象中的年份(四位数字)。
    setHours() 设置 Date 对象中的小时 (0 ~ 23)。
    setMilliseconds() 设置 Date 对象中的毫秒 (0 ~ 999)。
    setMinutes() 设置 Date 对象中的分钟 (0 ~ 59)。
    setMonth() 设置 Date 对象中月份 (0 ~ 11)。
    setSeconds() 设置 Date 对象中的秒钟 (0 ~ 59)。
    setTime() setTime() 方法以毫秒设置 Date 对象。
       
    <script type="text/javascript">
    
        var span = document.getElementById("spandate");
    
        var now = new Date();
    
        var year = now.getFullYear();//
        var month = now.getMonth() + 1;//月  (注意:月份+1)
        var date = now.getDate();//
       
        if (month < 10) {
            month = "0" + month;
        }
        if (date < 10) {
            date = "0" + date;
        }
       
        span.innerText = year + "-" + month + "-" + date;
       
    
    
    </script>
    js 中获取现在时间 年-月-日

    2、实时显示时间 

    <p id="time1" ></p>
    
    
    
    
    <script type ="text/javascript" >
    
        function mytime() {
    
            var a = new Date();
    
            var b = a.toLocaleTimeString();
    
            var c = a.toLocaleDateString();
    
            document.getElementById("time1").innerHTML = c + "&nbsp" + b;
    
        }
    
        setInterval(function () { mytime() }, 1000);
    
    </script>
    View Code

    toLocaleTimeString()

               根据本地时间把 Date 对象的时间部分转换为字符串,并返回结果。

                                e:   下午2:21:21

    toLocaleDateString()

              根据本地时间把 Date 对象的日期部分转换为字符串,并返回结果。

                               e:  2017/8/14

    <%@ page language="C#" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    
    
    <script type="text/javascript">
    
    function getTime() {
    
        var dateObj = new Date();
    
        var year = dateObj.getFullYear();//
    
        var month = dateObj.getMonth()+1;//月  (注意:月份+1)
    
        var date = dateObj.getDate();//
    
        var day = dateObj.getDay();
    
        var weeks = ["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
    
        var week = weeks[day];//根据day值,获取星期数组中的星期数。
    
        var hours = dateObj.getHours();//小时
    
        var minutes = dateObj.getMinutes();//分钟
    
        var seconds = dateObj.getSeconds();//
    
        if(month<10){
            month = "0"+month;
        }
        if(date<10){
            date = "0"+date;
        }
        if(hours<10){
            hours = "0"+hours;
        }
        if(minutes<10){
            minutes = "0"+minutes;
        }
        if(seconds<10){
            seconds = "0"+seconds;
        }
    
        var newDate = year+""+month+""+date+""+hours+":"+minutes+":"+seconds+"&nbsp &nbsp"+week;
    
        document.getElementById("date1").innerHTML = "时间显示:" + newDate;//在div中写入时间
    
        setTimeout('getTime()', 500);//每隔500ms执行一次getTime()
    
    }
    
    </script>
    
    
    <title>实时显示时间</title>
    </head>
    
    <body onload="getTime()">
    
        <div id="date1"></div>
    
    </body>
    
    </html>
    View Code

    获取到的月份加一

  • 相关阅读:
    1860 最大数
    1164 统计数字
    1063 合并果子
    1098 均分纸牌
    2806 红与黑
    1168 火柴棒等式
    1910 递归函数
    2774 火烧赤壁
    2017.0705.《计算机组成原理》-存储器
    2017.0704.《计算机组成原理》-动态RAM
  • 原文地址:https://www.cnblogs.com/Tanghongchang/p/7345327.html
Copyright © 2011-2022 走看看