0

I would like to begin by saying i looked at multiple threads in this forum before posting. Wasnt able to find my solution :(

Issue: getting a NaN error when trying to find the difference between two dates with a timestamp from two textboxes.

The date format i'm using is DDMMYYYY HH:MM - 27/01/2015 00:00 code below.

thank you in advance for this super helpful forum :)

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], timeParts[2]);

    return d;
}

function test() {
    var a = textbox_1.value;
    var b = textbox_2.value;
    alert(stringToDate(a) - stringToDate(b));
}
corvuscorax
  • 5,850
  • 3
  • 30
  • 31
marv
  • 962
  • 3
  • 9
  • 17

4 Answers4

1

Your date has / as separator but you are splitting the string on -. Change

var dateParts = s.split(' ')[0].split('-');

to

var dateParts = s.split(' ')[0].split('/');

Also, your time part has only hours and minutes, so there is no timeParts[2] present, just remove it from the setHours() call. Like this:

d.setHours(timeParts[0], timeParts[1])

Fiddle: http://jsfiddle.net/2evj59d1/

EDIT
Your code returns the difference in milliseconds. To convert it into date format just change

alert(stringToDate(a) - stringToDate(b));

to

alert(new Date(stringToDate(a) - stringToDate(b)));
Ganesh Jadhav
  • 2,830
  • 1
  • 20
  • 32
  • thank you for your response. I tried the code with begin/end dates as 27/01/2015 00:00 and 27/01/2015 00:01 respectively. the result i get is 60000. I dont know what to make of it. – marv Jan 29 '15 at 12:02
  • @marv: The response is in milliseconds. 60000 milliseconds = 60 seconds = 1 minute. – Ganesh Jadhav Jan 29 '15 at 12:07
  • yikes, that was dumb of me. so sorry. thank you so much for your help mate. I really appreciate it!! – marv Jan 29 '15 at 12:09
  • Thanks again. I tested and its working fine. sorry one more thing i missed. This doesn't calculate the date based on the hours. I tried 02/03/2015 18:26 - 30/01/2015 18:23 returned 3.0 minutes which is correct. It should say 1.03 ( 1 day 3 minutes). sorry completely forgot i also needed help excluding weekend calculation – marv Jan 29 '15 at 13:00
  • EDIT: I tried 29/01/2015 20:26 - 29/01/2015 20:25, and get 4:1. The one minute is correct, but the days are incorrect. Any clue? I'm using this code to convert to milliseconds: – marv Jan 29 '15 at 13:14
0

The code is trying to parse a time in the format HH:MM:SS. Skip the third part:

d.setHours(timeParts[0], timeParts[1]);
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
0

You can convert the date into milliseconds, get the difference and get the date back.

Fiddle

JSCode:

var a = new Date();
a.setDate(15);
a = a.getTime();
var b = new Date();
b.setDate(32);
b = b.getTime();
var c = b - a;
var date = new Date(c);
alert(date.getDate() - 1);
V31
  • 7,626
  • 3
  • 26
  • 44
  • thank you for your response. I tried your code and it appears to give me the correct result. this may be a simple question - how do i tweak the code to reference the textbox date values? – marv Jan 29 '15 at 12:01
0

for those who may have stumbled upon my post, i found my answer at the link below by user benjour. How do I get the difference between two Dates in JavaScript?

Community
  • 1
  • 1
marv
  • 962
  • 3
  • 9
  • 17