-1

I have the following code that now calculates the correct minutes, but the days does not calculate correctly. Also, needed help added code that will calculate mon-fri only and exclude sat/sun.

Date format I used subtract(DDMMYYY) : 01/01/2015 20:21 - 01/01/2015 20:22. This returns 4.1 using the code below. The ".1" is correct, one minute difference. The 4 is incorrect as its not more than a day.

Note - I searched other threads and couldnt find the solution. my code is below:

function stringToDate(s) {
    var dateParts = s.split(' ')[0].split('/'); 
    var timeParts = s.split(' ')[1].split(':');
    var d = new Date(dateParts[0], --dateParts[1], dateParts[2]);
    d.setHours(timeParts[0], timeParts[1]);
    return d; 
}

function test() {
    var a = slat_1.value;
    var b = slar_1.value;
    var x = (new Date(stringToDate(a) - stringToDate(b)));

    //converting milliseconds
    x = 1000*Math.round(x/1000); // round to nearest second
    var d = new Date(x);
    alert( d.getUTCDay() + '.' + d.getUTCMinutes() );
}
epascarello
  • 204,599
  • 20
  • 195
  • 236
marv
  • 962
  • 3
  • 9
  • 17
  • Check this post [Difference between date javascript] : http://stackoverflow.com/questions/3224834/get-difference-between-2-dates-in-javascript – FolabiAhn Jan 29 '15 at 15:58
  • 1
    Are you still having troubles with this script marv? [Answers](http://stackoverflow.com/questions/28213029/nan-javascript-error-when-calculating-between-dates-with-timestamp) from few hours ago didn't help? – skobaljic Jan 29 '15 at 16:02
  • possible duplicate of [How do I get the difference between two Dates in JavaScript?](http://stackoverflow.com/questions/41948/how-do-i-get-the-difference-between-two-dates-in-javascript) – Howard Renollet Jan 29 '15 at 16:02
  • Are you entering in `01/01/2015 20:21` into the input? If so, your date is wrong... – epascarello Jan 29 '15 at 16:17

3 Answers3

2

Well your order for the Date is wrong if you are entering in the format "01/01/2015 20:21"

var d = new Date(dateParts[0], --dateParts[1], dateParts[2]);

should be Year, Month, Date

var d = new Date(dateParts[2], --dateParts[0], dateParts[1]);

NEXT

var x = (new Date(stringToDate(a) - stringToDate(b)));

You are making a new date from subtracting two strings...Makes no sense.

Calculate the difference with basic math

function stringToDate(s) {
    var dateParts = s.split(' ')[0].split('/'); 
    var timeParts = s.split(' ')[1].split(':');
    var d = new Date(dateParts[2], --dateParts[0], dateParts[1]);
    d.setHours(timeParts[0], timeParts[1]);
    console.log(d)
    return d; 
}

(function test() {
    var a = "01/02/2015 20:21";
    var b = "01/02/2015 20:22";
    var delta  = (stringToDate(b) - stringToDate(a))/1000;

    var days = Math.floor(delta / 86400);
    delta -= days * 86400;
    console.log(delta);
    var minutes = Math.floor(delta / 60) % 60;
    alert(days + "." + minutes)
}());
epascarello
  • 204,599
  • 20
  • 195
  • 236
  • Thank you for your response. I tried the code and it works partially correct. the days calculate correctly but the time doesnt. e.g: i picked up two date values from a textbox - I tried 02/02/2015 12:00 - 02/03/2015 00:00, i got "0.0". This should have been ".05" days. – marv Feb 02 '15 at 13:27
1

The answer to all your problems (at least in this case) is to use momentjs and it's date calculation features.

moment.js

baao
  • 71,625
  • 17
  • 143
  • 203
  • even though moment.js is indeed an incredible library, it's kinda heavy and also this isn't an answer to the question and should in face be a comment – yuvi Jan 29 '15 at 16:12
-1

You may make your life easier by using Moment JS. It is hard to do math with dates, times and years in code. Some months have more days than others as well as some years. (Hello Leap Year)

I created a solution that worked on time and there are several issues that I noticed.

You can’t subtract the full date the way you have because when you move from one moth to the other you will be subtracting a smaller number from a larger one. And so you end up with a negative number. The same thing happens with the end of the year.

You may want to console log some of you variables to be sure you are getting what you are expecting.

That’s just based off what I see.

andre mcgruder
  • 1,120
  • 1
  • 9
  • 12