剩下的时间用Java语言编写? [英] Javascript for time left in the day?

查看:77
本文介绍了剩下的时间用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>

这篇关于剩下的时间用Java语言编写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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