33

is it possible to create a jQuery function so that it gets current date and time? I've been looking around documentation but haven't found anything so far...

Tsundoku
  • 9,104
  • 29
  • 93
  • 127
  • 12
    Sometimes people forget jQuery is just JavaScript... It's not the solution to everything, just shortcuts for complex operations. Simple things as getting current date and time should be done the easy way, as nickf answered. – Seb Mar 22 '09 at 00:13
  • 9
    I'm sure someone will come up with a jQuery Time plugin and a jQuery Basic Arithmetic plugin, to prevent us ever having to write a line of JavaScript without ‘$’ in it. – bobince Mar 22 '09 at 02:02
  • Dupe http://stackoverflow.com/questions/660927/how-can-i-obtain-the-local-time-in-jquery – gideon Dec 13 '10 at 18:44
  • 2
    @bobince: Solution: `window.$ = function (args) { eval(args); };`? – Hello71 Jan 05 '11 at 03:46

7 Answers7

47

@nickf's correct. However, to be a little more precise:

// if you try to print it, it will return something like:
// Sat Mar 21 2009 20:13:07 GMT-0400 (Eastern Daylight Time)
// This time comes from the user's machine.
var myDate = new Date();

So if you want to display it as mm/dd/yyyy, you would do this:

var displayDate = (myDate.getMonth()+1) + '/' + (myDate.getDate()) + '/' + myDate.getFullYear();

Check out the full reference of the Date object. Unfortunately it is not nearly as nice to print out various formats as it is with other server-side languages. For this reason there-are-many-functions available in the wild.

Paolo Bergantino
  • 480,997
  • 81
  • 517
  • 436
21

Yes, it is possible:

jQuery.now()

or simply

$.now()

see jQuery Documentation for jQuery.now()

Alex Doe
  • 359
  • 3
  • 12
20

You don't need jquery to do that, just javascript. For example, you can do a timer using this:

<body onload="clock();">

<script type="text/javascript">
function clock() {
   var now = new Date();
   var outStr = now.getHours()+':'+now.getMinutes()+':'+now.getSeconds();
   document.getElementById('clockDiv').innerHTML=outStr;
   setTimeout('clock()',1000);
}
clock();
</script>   

<div id="clockDiv"></div>

</body>

You can view a complete reference here: http://www.hunlock.com/blogs/Javascript_Dates-The_Complete_Reference

eKek0
  • 23,005
  • 25
  • 91
  • 119
7

It's plain javascript:

new Date()
nickf
  • 537,072
  • 198
  • 649
  • 721
4

Digital Clock with jQuery

  <script type="text/javascript" src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js?ver=1.3.2'></script>
  <script type="text/javascript">
  $(document).ready(function() {
  function myDate(){
  var now = new Date();

  var outHour = now.getHours();
  if (outHour >12){newHour = outHour-12;outHour = newHour;}
  if(outHour<10){document.getElementById('HourDiv').innerHTML="0"+outHour;}
  else{document.getElementById('HourDiv').innerHTML=outHour;}

  var outMin = now.getMinutes();
  if(outMin<10){document.getElementById('MinutDiv').innerHTML="0"+outMin;}
  else{document.getElementById('MinutDiv').innerHTML=outMin;}

  var outSec = now.getSeconds();
  if(outSec<10){document.getElementById('SecDiv').innerHTML="0"+outSec;}
  else{document.getElementById('SecDiv').innerHTML=outSec;}

} myDate(); setInterval(function(){ myDate();}, 1000); }); </script> <style> body {font-family:"Comic Sans MS", cursive;} h1 {text-align:center;background: gray;color:#fff;padding:5px;padding-bottom:10px;} #Content {margin:0 auto;border:solid 1px gray;width:140px;display:table;background:gray;} #HourDiv, #MinutDiv, #SecDiv {float:left;color:#fff;width:40px;text-align:center;font-size:25px;} span {float:left;color:#fff;font-size:25px;} </style> <div id="clockDiv"></div> <h1>My jQery Clock</h1> <div id="Content"> <div id="HourDiv"></div><span>:</span><div id="MinutDiv"></div><span>:</span><div id="SecDiv"></div> </div>

2

this is my way :

    <script type="text/javascript">
$(document).ready(function() {
    setInterval(function(){currentTime("#idTimeField")}, 500);
});
function currentTime(field) {
    var now = new Date();
    now = now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds();
    $(field).val(now);
}

it's not maybe the best but do the work :)

Anerty
  • 81
  • 1
  • 1
  • 9
0

Annoyingly Javascript's date.getSeconds() et al will not pad the result with zeros 11:0:0 instead of 11:00:00.

So I like to use

date.toLocaleTimestring()

Which renders 11:00:00 AM. Just beware when using the extra options, some browsers don't support them (Safari)

Documentation

Martin Lyne
  • 3,157
  • 2
  • 22
  • 28