需要帮助显示设置的动态日期和时间 [英] Need help displaying a set dynamic date and time

查看:73
本文介绍了需要帮助显示设置的动态日期和时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我希望我的网站显示我当地的时间和日期供大家看。



假设目前于2014年12月17日下午6:25 在我的时区以及访问者该网站的时间和日期同时<2014年12月18日下午2:25 。我基本上试图做的是让我的网站在该实例中显示 12/17/14 6:25:?? PM ?? 是秒,但时间和日期必须是动态的,并由第二个自己更新。



我非常感谢能够完成这项工作的一些帮助,我希望这一次我已经足够具体,但如果我没有请让我知道我可以这样,这不会再被阻止。

解决方案

呃,你的问题有点含糊。你只需要一个带日期的时钟显示在你的网页上?除非你提供了一些代码,否则我不能将它添加到你现有的项目中,但我用HTML编写了一个工作代码。从W3School的一些灵感( http://www.w3schools.com ),我最终得出了这个结论:

 <!DOCTYPE html> 
< html>
< head>
< script>
函数startTime(){
var today = new Date();
var day = today.getDate();
var month = today.getMonth();
var year = today.getFullYear();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
m = checkTime(m);
s = checkTime(s);
document.getElementById('txt')。innerHTML =Date:+ day +/+ month +/+ year ++Clock:+ h +:+ m +: + S;
var t = setTimeout(function(){startTime()},500);
}

函数checkTime(i){
if(i <10){i =0+ i}; //在数字前面加零10
返回i;
}
< / script>
< / head>

< body onload =startTime()>

< div id =txt>< / div>

< / body>
< / html>

这是你的意思吗?

编辑只是我做了日期的变量。

Basically I want my website to display my local time and date for everyone to see.

Let's say it is currently 6:25 PM on December 17, 2014 in my time zone and a visitor to the site's time and date is at the same time 2:25 PM on December 18, 2014. What I am basically trying to do is have my website display in that instance 12/17/14 6:25:?? PM with ?? being the seconds but the time and date must be dynamic and update on its own by the second.

I would very much appreciate some help in being able to get this done and I hope I have been specific enough this time but if I haven't please let me know how I can be so this doesn't get blocked again.

解决方案

Uhm, your question is a bit vague. You just want a clock with date to be shown on your webpage? Unless you provide some code, I can't add it to your existing project, but I made a working code in HTML. With some inspiration from W3School (http://www.w3schools.com) I ended up with this:

<!DOCTYPE html>
<html>
    <head>
        <script>
            function startTime() {
                var today=new Date();
                var day = today.getDate();
                var month = today.getMonth();
                var year = today.getFullYear();
                var h=today.getHours();
                var m=today.getMinutes();
                var s=today.getSeconds();
                m = checkTime(m);
                s = checkTime(s);
                document.getElementById('txt').innerHTML = "Date: " + day + "/" + month + "/" + year + " " + "Clock: " + h+":"+m+":"+s;
                var t = setTimeout(function(){startTime()},500);
            }

            function checkTime(i) {
                if (i<10) {i = "0" + i};  // add zero in front of numbers < 10
                return i;
            }
        </script>
    </head>

    <body onload="startTime()">

        <div id="txt"></div>

   </body>
</html>

Is this what you mean?

The edit is just that I made variables of the date too.

这篇关于需要帮助显示设置的动态日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆