1

I have two dates using the obout controls, when both dates are entered instead of having the user enter the total number of days field text box, I wish to have a piece of javascript that would automatically calculate the total number from looking at the start and finish date entered.With a label displaying the calculated total days value.So far it is not generating anything in my label, but it runs ok so im guessing im not far off.

Here is my javascript

  var startDate = new Date(document.getElementByName('txtstartdate').Value);
        var endDate = new Date(document.getElementByName('TxtFinish').Value);

        var days = 24 * 60 * 60 * 1000; 
        var distance = (endDate - startDate) / days;


        var lbltotal = $get('<%= lbltotal %>');
        var lbltotal = lbltotal.innerText;
madzcoding
  • 94
  • 1
  • 4
  • 12

1 Answers1

3

Looking at this: Date subtraction in JavaScript

It seems you could accomplish this by something like this:

var startDate = new Date(document.getElementByName('Calendar1').Value);
var endDate = new Date(document.getElementByName('Calendar2').Value);

var days = 24 * 60 * 60 * 1000; 
var distance = (endDate - startDate) / days;

Since you will get your answer in miliseconds, use the variables days (24hrs x 60 minutes x 60 seconds x 60 milliseconds) to determine how many 24 hour periods you have.

EDIT To put the value into the ASP label check here: Why is javascript unable to get asp:Label innerText?

Community
  • 1
  • 1
NealR
  • 10,189
  • 61
  • 159
  • 299
  • I used your code but my label is not generating the total days but i dont get an error so I think im not far off – madzcoding Sep 04 '12 at 09:01