views:

54221

answers:

9

Something similar to Unix's timestamp, that is a single number that represents the current time and date. Either as a number or a string.

+89  A: 
new Date().getTime(); // returns the number of MS since the epoch
daveb
If you want it similar to Unix timestamp you probably want it in seconds : Math.round(new Date().getTime() / 1000)
radius
I like `+new Date()`, more badass, or even if it's just to confuse the JS newbies. (coherces Date object into Number)
Infinity
Should it be in UTC? If so how is it converted?
Adam
+2  A: 
var $time = Date.now || function() {
  return +new Date;
};

$time()
Staale
Why the || operator? Is Date.now() not available on all browsers?
Chris Noe
Apparently not, I found the code in modulejs
Staale
+3  A: 
var timestamp = Number(new Date()); // current time as number
aemkei
A: 
new Date().valueOf()// returns the number of MS since the epoch
Tom Viner
+2  A: 

I don't have enough points to add a comment to daveb's accepted answer but I think it's worth saying, so I hope you don't mind me putting it here.

One benefit of his way is that the same syntax can be found in other languages. For instance, the following code will give you the exact same timestamp in Java or Scala:

(new java.util.Date()).getTime();

pr1001
The question is about JavaScript not Java..
dejavu
+1  A: 
time = Math.round(((new Date()).getTime()-Date.UTC(1970,0,1))/1000);
Kiragaz
A: 

Is there a solution to disable caching when making the POST/GET

Yes, use jQuery and have a look at the Ajax tutorials.

Jason
Does IE cache POST requests? I considered going that route but heard that POST actually generates two requests.
Drew
A: 
    var d=new Date();
    var timestamp = new Array(2);
    timestamp[1] = d.getDate();
    timestamp[2] = d.getMonth();
    document.write(timestamp[1]);
    switch (timestamp[2])
      {
      case 0: document.write("Jan");
      break;
      case 1: document.write("Feb");
      break;
      case 2: document.write("Mar");
      break;
      case 3: document.write("Apr");
      break;
      case 4: document.write("May");
      break;
      case 5: document.write("June");
      break;
      case 6: document.write("July");
      break;
      case 7: document.write("Aug");
      break;
      case 8: document.write("Sept");
      break;
      case 9: document.write("Oct");
      break;
      case 10: document.write("Nov");
      break;
      case 11: document.write("Dec");
      break;
      }
Douglas Gross
A: 

alright, thats what iam looking for, its the timestamp like php, thx

Björn
time = Math.round(((new Date()).getTime()-Date.UTC(1970,0,1))/1000);i mean ...
Björn