-4

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?

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

Thanks

just somebody
  • 18,602
  • 6
  • 51
  • 60
Rohit
  • 685
  • 3
  • 13
  • 28
  • possible duplicate of [jQuery getTime function](http://stackoverflow.com/questions/670290/jquery-gettime-function) – Sergio Jul 26 '13 at 16:36
  • text in front it? for what? – svillamayor Jul 26 '13 at 16:39
  • Please solve that question http://stackoverflow.com/questions/17881261/how-to-show-time-in-front-of-web-service-data-in-jquery-mobile/17881466?noredirect=1#17881466 – Rohit Jul 26 '13 at 16:58

2 Answers2

10

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

  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:

  $('#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>');
tessaract
  • 211
  • 1
  • 6
  • Now my real question is this .. http://stackoverflow.com/questions/17881261/how-to-show-time-in-front-of-web-service-data-in-jquery-mobile/17881466?noredirect=1#17881466 – Rohit Jul 26 '13 at 16:55
  • Actually i am getting a data from server after a few seconds But it is not sending any time .so i need to show time with data in my screen .So i need to make one div in which i have two div one div have current time and other is data .may i right .can you make any other structure? – Rohit Jul 26 '13 at 16:57
  • text are not in same line..:( here is fiddle http://jsfiddle.net/DhneL/ – Rohit Jul 26 '13 at 17:08
8

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/

Josh
  • 2,835
  • 1
  • 21
  • 33
  • As I told you I have big question i am breaking this questions in part why you are not understand..!! – Rohit Jul 26 '13 at 16:49
  • Now I want to show some text in front of this time .why this is not horizontal ?this fiddle http://jsfiddle.net/u32pU/1/ – Rohit Jul 26 '13 at 16:52
  • Try this: http://jsfiddle.net/u32pU/2/ – Josh Jul 26 '13 at 16:54
  • Please solve this question same as http://stackoverflow.com/questions/17881261/how-to-show-time-in-front-of-web-service-data-in-jquery-mobile/17881466?noredirect=1#17881466 – Rohit Jul 26 '13 at 16:58