Javascript 使用 moment.js 和 setInterval 的动态日期和时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10590461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dynamic date and time with moment.js and setInterval
提问by bchr
I'm trying to find out how I can display dynamic date and time using moment.js.
我试图找出如何使用moment.js显示动态日期和时间。
Apparently I can't figure out to use setInterval properly.
显然我不知道如何正确使用 setInterval 。
If possible I'd prefer not to use jQuery as moment.js dosn't need it.
如果可能的话,我不想使用 jQuery,因为 moment.js 不需要它。
Here's what I have so far: http://jsfiddle.net/37fLF/2/.
这是我到目前为止所拥有的:http: //jsfiddle.net/37fLF/2/。
$(document).ready(function () {
var datetime = $('#datetime'),
date = moment(new Date()),
update = function(){
datetime.html(date.format('dddd, MMMM Do YYYY, h:mm:ss a'));
};
update();
setInterval(update, 1000);
});?
回答by MilkyWayJoe
I've made a few modifications in your code:
我对你的代码做了一些修改:
Note that the method update is now outside the ready
event handler
请注意,方法更新现在在ready
事件处理程序之外
code:
代码:
var datetime = null,
date = null;
var update = function () {
date = moment(new Date())
datetime.html(date.format('dddd, MMMM Do YYYY, h:mm:ss a'));
};
$(document).ready(function(){
datetime = $('#datetime')
update();
setInterval(update, 1000);
});
working demo: jsfiddle
工作演示:jsfiddle
回答by bchr
Again, thanks for your answers. The code I ended up with follows. (Note danish i18n)
再次感谢您的回答。我最终得到的代码如下。(注意丹麦 i18n)
moment.lang('da');
var datetime = null, date = null;
var datetime_update = function() {
date = moment(new Date());
datetime.html(date.format('[Lige nu: ] dddd [d.] Do MMMM YYYY [kl.] HH:mm:ss'));
};
$(document).ready(function() {
datetime = $('#datetime');
datetime_update();
setInterval(datetime_update, 1000);
});
EDIT: Here nine months after I asked this quesiton I figured out this way to do it without jQuery (as I initially asked). Here it is:
编辑:在我问这个问题九个月后,我想出了不用 jQuery 的方法(正如我最初问的那样)。这里是:
function date_time() {
now = moment().format('dddd [d.] Do MMMM YYYY [kl.] HH:mm:ss');
document.getElementById('timer').innerHTML = now;
setTimeout(function () { date_time(); }, 1000);
}
date_time();
And then of course use it with an HTML ID like:
然后当然可以将它与 HTML ID 一起使用,例如:
<span id="timer"></span>
回答by Halcyon
Put
放
date = moment(new Date());
inside the update function. Now you're just printing the same date every second ;)
在更新函数里面。现在你只是每秒打印相同的日期;)
回答by Muhammad Hamayoon
This is how it goes by me and its working u can check it here
这就是我的工作方式及其工作方式,您可以在此处查看
HTML
< asp:Label ID="Label1" runat="server" Text='' >
< asp:Label ID="Label1" runat="server" Text='' >
JavaScript
< script language="javascript" type="text/javascript">
function date_time()
{
var dt = new Date();
document.getElementById('<%=Label1.ClientID%>').innerHTML = dt;
setTimeout(function () { date_time(); }, 1000);
}
date_time();
< /script>