JavaScript
  Home arrow JavaScript arrow Page 2 - Quick and Dirty Date Differences with Java...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Quick and Dirty Date Differences with JavaScript
By: JavaScript Kit
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 9
    2003-07-07

    Table of Contents:
  • Quick and Dirty Date Differences with JavaScript
  • The Article

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Quick and Dirty Date Differences with JavaScript - The Article


    (Page 2 of 2 )

    Lets begin this tutorial by getting to the heart of it:

    Date.getTime()

    Date.getTime() is a prebuilt JavaScript method that returns the time elapsed from January 1st, 1970 to the current Date instance, in milliseconds. And we all know the easiest subjects to perform arithmetic on are numbers.

    So here's the general premise for calculating the difference between two dates:
     
    1) Convert both dates to a number using Date.getTime()
    2) Then subtract!

    On to a few examples now…

    Calculating days remaining until Christmas

    <script type="text/javascript">

    //Set the two dates
    today=new Date()
    var christmas=new Date(today.getFullYear(), 11, 25) //Month is 0-11 in JavaScript
    //Set 1 day in milliseconds
    var one_day=1000*60*60*24

    //Calculate difference btw the two dates, and convert to days
    document.write(Math.ceil((christmas.getTime()-today.getTime())/(one_day))+
    " days left until Christmas!")

    </script>

    Notice how the year for "Christmas" is dynamically set to the current year, so the script is reusable now and in the future as well without having to modify it. Our annual events countdown script uses this concept to count down to any annual event (ie: a holiday, birthday, etc) of your choosing.

    Dynamically indicating what's new on your page

    Moving on, how about displaying a "new" image alongside new content that will automatically disappear (the image, that is) after the specified future date has been reached? The logic is simple enough -- if the current date is less than the specified future date, write out the "new" image:

    <script type="text/javascript">

    var newimage='<img src="news.gif">'
    var today=new Date()

    function whatsnew(yr,mon,day){
    var expire=new Date(yr,mon,day)
    if (today.getTime()<=expire.getTime())
    document.write(newimage)
    }

    </script>

    <!--"New" image will disappear after Dec 30th, 2002-->
    <script> whatsnew(2003,11,30) </script> This is new content!


    Calculating time expired since a particular past date

    Finally, it's just as easy to count up as it is to count down. The following JavaScript examples shows how many days has elapsed since the launch of our site:

    <script type="text/javascript">

    //Set the two dates
    var millennium =new Date(1997, 5, 11) //Month is 0-11 in JavaScript
    today=new Date()
    //Get 1 day in milliseconds
    var one_day=1000*60*60*24

    //Calculate difference btw the two dates, and convert to days
    document.write(Math.ceil((today.getTime()-millennium.getTime())/(one_day))+
    " days has gone by since JavaScriptKit.com started!")

    </script>

    Yes, this tutorial is short and to the point, and hopefully I’ve provided you with enough source code to feel comfortable dabbling with dates in JavaScript!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

    JAVASCRIPT ARTICLES

    - More on JavaScript Array Objects
    - Methods of the DOM Location Object
    - The DOM Location Object Properties
    - Handling Remote Files with JavaScript Click ...
    - Using Click Interceptions with a Database-Dr...
    - Using JavaScript Click Interceptions in an I...
    - Using Click Interceptions with JavaScript
    - QuickSort in Action
    - Quicksort
    - Using Mod_Security to Protect Your Server
    - Detecting and Countering Server Intrusions
    - Securing Your Web Server
    - Building a Secure Web Server
    - Protecting the Server
    - Book Review: Learning the Yahoo! User Interf...


     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT