What's the best way to calculate date difference in Javascript
-
I doing a function in Javascript like the VisualBasic DateDiff. You give two dates and the returning time interval (Seconds, Minutes, Days, etc...) DateDiff(ByVal Interval As Microsoft.VisualBasic.DateInterval, _ ByVal Date1 As Date, ByVal Date2 As Date) as Long So what's the best way to calculate the difference of Javascript Dates?
-
Answer:
Use the http://w3schools.com/jsref/jsref_obj_date.asp like so: function DateDiff(var /*Date*/ date1, var /*Date*/ date2) { return date1.getTime() - date2.getTime(); } This will return the number of milliseconds difference between the two dates. Converting it to seconds, minutes, hours etc. shouldn't be too difficult.
DK39 at Stack Overflow Visit the source
Other answers
If you follow this http://www.javascriptkit.com/javatutors/datedifference.shtml, one way is to use: Date.getTime() You will find a full http://javascript.internet.com/math-related/date-difference.html, complete with date validation.
VonC
Javascript source code to do exactly that can be found http://javascript.internet.com/math-related/date-difference.html
Paul
Related Q & A:
- What's the best way to get into the Graphic Design industry?Best solution by Yahoo! Answers
- What's the best way of returning damaged NIKE shoes to NIKE for another pair?Best solution by Yahoo! Answers
- What's the best way to hook up an overhead projector to a laptop?Best solution by Yahoo! Answers
- What's the best way to whiten your teeth?Best solution by Yahoo! Answers
- What's the best way to start a small clothing line business?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.