Javascript time difference

Hello,

I am new at Javascript.

How would I calculate the number of seconds between a constant datetime and the current datetime?

Thanks!

Drew
drew17Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

OmniUnlimitedCommented:
var d1 = new Date();  // get today's date
var t1 = d1.getTime(); // change to milliseconds since 01/01/1970
var d2 = new Date("date-to-compare");
var t2 = d2.getTime(); // change to milliseconds
// Do the subtraction and multiply by 1000
var sec = (t1-t2) * 1000;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Pierre CorneliusCommented:
@omni

Shouldn't that be divide by 1000?
if t1 and t2 are time in milliseconds then to get to seconds you should divide by 1000, not multiply by 1000.

Also wrapping in a function would be nice :)

e.g.

function SecondsSince(date_from) {
  var d1 = new Date();  // get today's date
  var t1 = d1.getTime(); // change to milliseconds since 01/01/1970
  var d2 = new Date(date_from);
  var t2 = d2.getTime(); // change to milliseconds
  // Do the subtraction and divide by 1000 to convert from milliseconds to seconds
  return (t1-t2) / 1000;
}
0
OmniUnlimitedCommented:
LOL!  Yes PierreC, thank you for catching my faux pas.  It should be divided by 1000, not multiplied by 1000.

@drew17: please see the comment by PierreC for the correct rendition of the code.
0
Alexandre SimõesManager / Technology SpecialistCommented:
Here's my solution in one line:

var diff = Math.floor((+new Date() - msYourDate) / 1000);

You can see it working here:
http://jsfiddle.net/jAPCG/

Remarks:
1. I'm rounding down (floor)
2. Putting a + before the date converts it to milliseconds
3. Keep in mind that months in javascript begin in 0
     - you can see this in my example on jsFiddle
     - might not be important to you but I thought I should remark it

Cheers!
0
drew17Author Commented:
Thank you all!

For the knowledge base, my code is:

  var d1 = new Date();  // get today's date
  var t1 = d1.getTime(); // change to milliseconds since 01/01/1970
  var d2 = new Date('2013-09-17 00:00:00');
  var t2 = d2.getTime(); // change to milliseconds
  // Do the subtraction and divide by 1000 to convert from milliseconds to seconds
  var timeDiff = (t1-t2) / 1000;

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.