Important alert: (current site time 7/15/2013 12:31:01 PM EDT)
 

article

Calculate difference between two dates

Email
Submitted on: 6/23/2004 12:47:22 AM
By: Super Mario 
Level: Beginner
User Rating: By 1 Users
Compatibility: JavaScript
Views: 17095
 
     See how to calculate the difference between two dates in JavaScript. It's easy.

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				

Calculate difference between two dates

In JavaScript, you can easily get the difference between two dates. This is because when you create a date object, it's automatically converted to a number, specifically, the number of milliseconds passed since 1/1/1970 GMT to the date. So here's an example that shows how many days have passed since January 1st, 1998:

<script>
var today=new Date()
var day1998=new Date(1998,0,1)
var difference=today-day1998//unit is milliseconds
formatdifference=Math.round(difference/1000/60/60/24) //now unit is days
document.write(formatdifference)
</script>

Easy enough right? One script I like that displays the difference between two dates in a variety of units is The Ultimate Age calculator: http://www.javascriptkit.com/script/script2/calculateage.shtml


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

11/25/2004 6:04:29 AM

how are you from wher are yopu
(If this comment was disrespectful, please report it.)

 
4/6/2007 12:46:44 AMvenugopal

Excellent Opportunities for all netizens
Thank QQQQQQQQ So Much.........
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.