0

I have start date and end date with start time and end time. I have start date as a one string,end date as another string, start time as a string and end time as string. I need to find out the hours between two days

   `var start_time = "12-05-2015";
     var end_time ="14-05-2015";
    var start_time_split ="05:30";
    var end_time_split ="22:30";`

How can I find ??

Aravind E
  • 1,031
  • 3
  • 15
  • 25
  • Try looking into something like `a = new Date(timestr)` combined with the `getTime()` method – EdgeCaseBerg May 21 '15 at 13:49
  • This link ( http://stackoverflow.com/questions/20839874/difference-between-two-dates-in-minute-hours-javascript ) i believe would be something close to what you need . – cs04iz1 May 21 '15 at 14:01

1 Answers1

1

I would use the valueOf() function which removes any possible errors due to daylight saving time etc - it converts dates to a number of milliseconds since 1970. But you;ll need to format your date in an unambiguous way, 12-05-2015 will be taken as 5th December 2015 . Use '2015-05-12' for 12th May. Code below can be shortened a lot but shows you what's going on.

var start_time = "2015-05-12";
var end_time ="2015-05-14";
var d1= new Date(start_time);
var d2 = new Date(end_time);
var ms1 = d1.valueOf();
var ms2 = d2.valueOf();
var diff_ms = ms2-ms1;
var diff_hrs = diff_ms / 3600000;
alert(diff_hrs);

See this fiddle

quilkin
  • 874
  • 11
  • 31
  • Thanks quilkin. I need to calculate time given by the user also, in your fiddle example i dint see the user time.. i need the user time to append with the date which i have given. – Aravind E May 22 '15 at 04:39
  • OK, I've updated the fiddle. But you will need to use one of the accepted date formats (as I have shown); your original format of '12-05-2015' won't work. – quilkin May 22 '15 at 08:39
  • Ok, please can you mark it as answered and/or up-vote my answer, so that I get some brownie points! Thanks – quilkin May 22 '15 at 12:31