Click here to Skip to main content
13,556,972 members
Click here to Skip to main content
Add your own
alternative version

Stats

137.4K views
12 bookmarked
Posted 8 Apr 2007
Licenced

How to calculate difference between two dates using JavaScript

, 8 Apr 2007
Rate this:
Please Sign up or sign in to vote.
This code will give you the diffrence between the two dates in days.Some time it is required in the program to get diff. between two dates.It is so small and very efficient code.

Introduction

This code will give you the diffrence between the two dates in days.Some time it is required in the program to get diff. between two dates.It is so small and very efficient code.

Background

(Optional) Is there any background to this article that may be useful such as an introduction to the basic ideas presented?

Using the code

A brief desciption of how to use the article or code. The class names, the methods and properties, any tricks or tips.

Blocks of code should be set as style "Formatted" like this:

//
// Any source code blocks look like this
//
        t1="10/10/2006" ;

        t2="15/10/2006";

 
   //Total time for one day
        var one_day=1000*60*60*24; 
//Here we need to split the inputed dates to convert them into standard format
for furter execution
        var x=t1.split("/");     
        var y=t2.split("/");
  //date format(Fullyear,month,date) 

        var date1=new Date(x[2],(x[1]-1),x[0]);
  
        var date2=new Date(y[2],(y[1]-1),y[0])
        var month1=x[1]-1;
        var month2=y[1]-1;
        
        //Calculate difference between the two dates, and convert to days
               
        _Diff=Math.ceil((date2.getTime()-date1.getTime())/(one_day)); 
//_Diff gives the diffrence between the two dates.

Remember to set the Language of your code snippet using the Language dropdown.

Use the "var" button to to wrap Variable or class names in <code> tags like this.

Points of Interest

Did you learn anything interesting/fun/annoying while writing the code? Did you do anything particularly clever or wild or zany?

History

Keep a running update of any changes or improvements you've made here.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Nilesh Surve
Software Developer (Senior) Panjim,Goa
India India
Nilesh Surve
Software Engineer
Panjim,Goa
India.

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralBest sol Pin
Rohan Kishor Garud20-Nov-14 18:23
professionalRohan Kishor Garud20-Nov-14 18:23 
GeneralNice job! Pin
jpinto348817-Mar-11 10:08
memberjpinto348817-Mar-11 10:08 
GeneralIt works well,but Improve the vizualization Pin
rokkiya29-Apr-09 0:49
memberrokkiya29-Apr-09 0:49 
GeneralMy vote of 2 Pin
rokkiya29-Apr-09 0:45
memberrokkiya29-Apr-09 0:45 
GeneralIt's not working Pin
chintans8-Oct-07 23:18
memberchintans8-Oct-07 23:18 
GeneralRe: It's not working Pin
dotnetpickles12-Feb-14 0:53
memberdotnetpickles12-Feb-14 0:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02-2016 | 2.8.180515.1 | Last Updated 8 Apr 2007
Article Copyright 2007 by Nilesh Surve
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid