473,508 Members | 2,384 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

calculating difference of dates

6 New Member
hi,

iam struggling with difference between two dates of format type dd/mm/yyyy

i need difference between two dates olddate(dd/mm/yyyy) - newdate(dd/mm/yyyy) and this should be in java script please helpme


thanking you
Sep 6 '07 #1
3 1781
ak1dnar
1,584 Recognized Expert Top Contributor
If you need it from JAVASCRIPT ask the question in this forum

Moving from Php forum.
Sep 6 '07 #2
phvfl
173 Recognized Expert New Member
Hi,

The function that you would be looking to use would be the setFullYear() function of the Date object in javascript. setFullYear() sets the date value, taking three variables (four digit year, month and day). The month is base 0 so Janary would be 0, for example:

Expand|Select|Wrap|Line Numbers
  1. var date1 = new Date();
  2. date1.setFullYear(2007,0,1);
  3.  
would set date1 to the 1st January 2007. The dates are actually stored as integers and their base value is the number of milliseconds since 1st January 1970, so if you subtract one date from another you would get the difference in time in milliseconds. Once you have this it would be simple arithmetics to convert the difference into whatever unit of time that you want.

If you are still having difficulties please post back your attempted code to look at.
Sep 6 '07 #3
mrhoo
428 Contributor
Expand|Select|Wrap|Line Numbers
  1. function daysbetween(s1,s2){
  2.     /*
  3.     First, convert your strings to javascript Date objects.
  4.     Reverse the order from dd/mm/yyyy to yyyy/mm/dd,
  5.      and subtract 1 from the month field
  6.      (months are 0-based, january is 0)
  7.     */
  8.  
  9.     var d1= s1.split(/\W+/).reverse();
  10.     var d2= s2.split(/\W+/).reverse();
  11.  
  12.     d1= new Date(d1[0],d1[1]-1,d1[2]);
  13.     d2= new Date(d2[0],d2[1]-1,d2[2]);
  14.  
  15.     //Now you can subtract the two dates and return the difference in days
  16.  
  17.     return Math.floor((d2-d1)/8.64e7)+ ' d
  18. }
  19. // Test Case
  20.  
  21. var s1= '06/09/2007';
  22. var s2= '25/12/2007';
  23. alert(daysbetween(s1,s2))
Sep 6 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
4436
by: Hans Gruber | last post by:
Hi all, I have been struggling with a problem all day, I have been unable to come up with a working solution. I want to write a function which takes 2 unix timestamps and calculates the...
10
6617
by: riki | last post by:
hello, i need to calculate num of days between 2 dates... i get separate parts of dates from html form, then i need to "make" begining and ending date and calculate difference between them......
2
2674
by: Scott | last post by:
Hi All I have an application that has a start date and a finish date I need to calculate the number of days from the start date to the end date and then display this in a read only text box... ...
2
2082
by: Carl | last post by:
Hi, I have a query that produces an output similar to the sample shown below. These records are based on certain criteria and a date range selected from a form. I need to create some kind of...
4
1476
by: Yotam | last post by:
Hi, I need some help with JS. I will be grateful, if you can help me out. I have two date fields (check in, check out) and "number of days" field. I want the script to calculate automatically...
5
3474
by: Julius | last post by:
Hej dudes, I need to calc the difference between two timestamps / dates ... For example what i need to calculate: Date 1: 2007.11.06 - 20:13:04 Date 2: 2007.11.07 - 21:13:04 Difference:...
6
8995
by: krishnakant Mane | last post by:
hello, I am strangely confused with a date calculation problem. the point is that I want to calculate difference in two dates in days. there are two aspects to this problem. firstly, I can't get...
2
5832
by: ncsthbell | last post by:
I am having problems getting the end date to calculate correctly. I start with Quarter '03/02', (YY/QTR), for this it means it is for the 2nd qtr of 2003. My goal is to get the begin & end dates...
3
1730
by: mfaisalwarraich | last post by:
Hi everybody. I have some problem while calculating time. i have employees who are working on hourly basis. i want to calculate the time. for example if an employee start working at 11:00pm...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7115
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7321
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5624
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5047
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4705
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
414
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.