如何在 jquery mobile 或 jquery 中获取当前时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17886560/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:09:48  来源:igfitidea点击:

How to get current time in jquery mobile or jquery

jqueryjquery-mobile

提问by Rohit

Can you please tell me how to get current time? I want to display current time with text in front of it.

你能告诉我如何获得当前时间吗?我想在它前面显示带有文本的当前时间。

Example:

例子:

I have to take one div tag in which i have one label (having current time) front of that i want to show some text. How to do that?

我必须采用一个 div 标签,其中我有一个标签(具有当前时间),我想在其中显示一些文本。怎么做?

Actually my problem is different. I am solving the problem in breaking parts.

其实我的问题是不同的。我正在解决零件损坏的问题。

Thanks

谢谢

回答by tessaract

You'd probably do best just using regular JavaScript for the time: The date object has a getTime method: http://www.w3schools.com/jsref/jsref_gettime.asp

您可能最好只使用常规 JavaScript 时间:日期对象有一个 getTime 方法:http: //www.w3schools.com/jsref/jsref_gettime.asp

  var currentTime = new Date()
  var hours = currentTime.getHours()
  var minutes = currentTime.getMinutes()


  if (minutes < 10)
  minutes = "0" + minutes

Then, using jQuery you could display it in your div:

然后,使用 jQuery 您可以在您的 div 中显示它:

  $('#divID').html("<b>" + hours + ":" + minutes + " " + "</b>");

You can then prepend any text you want in front of that:

然后,您可以在其前面添加您想要的任何文本:

  $('#divID').prepend('<p>Text before timestamp<p>');

回答by Flame Trap

Do you mean something like this:

你的意思是这样的:

function date() {
    var now = new Date(),
        now = now.getHours()+':'+now.getMinutes()+':'+now.getSeconds();
    $('#time').html(now);
}

Demo: http://jsfiddle.net/u32pU/

演示: http://jsfiddle.net/u32pU/