剩下的时间用Java语言编写?
我了解如何在一定程度上(到指定日期,即YYYY-MM-DD)使用倒数计时器和日期计时器,但是我正在从事一个Web开发学院项目,希望在其中开发一个网页(JSP)文件)以设置倒计时计时器,其中包含从Web应用程序启动之日起剩余的秒数.
I understand how to work countdown timers and date timers to an extent (to a specified date i.e YYYY-MM-DD) but I'm working on a web development college project where I wish for one of my web pages (JSP file) to have a countdown timer with the number of seconds left in the day from when the web application launches.
该网页还将包含一个Ajax函数,用户可以在其中单击按钮,并且会出现随机激励消息(我知道这段特殊代码,但这只是为了让您了解为什么我想要此倒数计时器).
The web page will also include an Ajax function where the user can click a button and a random motivational message will appear (this particular piece of code I know, but it's just to give you an idea of why I want this countdown timer).
Moment.js是一个很好的日期数学库. http://momentjs.com/
Moment.js is a great library for date math. http://momentjs.com/
使用Moment,您可以像这样制作一根内胆.
Using Moment, you could do a one liner like this.
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.4/moment.min.js"></script>
<script>
document.write(moment.duration(moment().add(1, 'day').startOf('day').diff(moment())).asSeconds())
</script>
或更容易理解的版本:
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.4/moment.min.js"></script>
<script>
var now = moment(),
tomorrow = moment().add(1, 'day').startOf('day'),
difference = moment.duration(tomorrow.diff(now))
document.write(difference.asSeconds())
</script>