Wrox Programmer Forums > Java How to calculate a number of days between 2 date
 | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
 Pro JSP Advanced JSP coding questions. Beginning questions will be redirected to the Beginning JSP forum.
 Welcome to the p2p.wrox.com Forums. You are currently viewing the Pro JSP section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersâ€™ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
July 20th, 2006, 03:47 AM
 Registered User Join Date: Jul 2006 Location: , , . Posts: 2 Thanks: 0 Thanked 0 Times in 0 Posts
How to calculate a number of days between 2 date

Dear all,

I have the following script:

var Day1 = dateObj1.value.substring( 0, 2 );
var Month1 = dateObj1.value.substring( 3, 5 );
var Year1 = dateObj1.value.substring( 6, 10 );
var Dt1 = Year1 + Month1 + Day1;

var Day2 = dateObj2.value.substring( 0, 2 );
var Month2 = dateObj2.value.substring( 3, 5 );
var Year2 = dateObj2.value.substring( 6, 10 );
var Dt2 = Year2 + Month2 + Day2;

if( Dt1 > Dt2 || Dt1 == "" || Dt2 == "")
{

return true;

}

For example, if Dt1 value is 20050601 and Dt2 value is 20050523, how can i get the number of days?. besides that, i also want to check the condition if Dt1 value <=Dt2 + 60 prompt messgae"ok" otherwise prompt message "<= 60days of PPC date"

Any helps is highly appreciated.

July 20th, 2006, 07:09 PM
 Friend of Wrox Join Date: Jan 2006 Location: San Francisco, CA, USA. Posts: 198 Thanks: 0 Thanked 0 Times in 0 Posts

Your script can be highly simplified to just:

if (dateObj1.after(dateObj2)) return true;

To calculate the number of days between the two objects, you can do date math on the Date.getTime() return value:

int days = (dateObj1.getTime() - dateObj2.getTime()) / (1000 * 60 * 60 * 24);

If dateObj1 is not necessarily after dateObj2, you may want to use Math.abs(int) to always get a positive number.

Jon Emerson

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is OffTrackbacks are Off Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Calculate # of work days ? kscase SQL Server 2005 3 June 9th, 2007 09:11 PM Todays date and date of 3 days later Singh591 Visual Basic 2005 Basics 1 December 21st, 2006 06:06 PM Calculating number of days between multiple dates Vann Access 4 December 3rd, 2004 08:26 PM Calculate Business Days - MS access 97 snoopy92211 Access VBA 0 October 13th, 2004 02:13 PM How to calculate the work days beetwen two dates. andres_pm Javascript 1 October 7th, 2004 06:23 AM

All times are GMT -4. The time now is 04:00 AM.