0

http://jsfiddle.net/uWT4r/131/

I need the time in 12 hr format

If I enter

in first input field (start time) 12:00 AM and

in second input field (end time) 1:45 PM and on click

in third input field time should be 01:45

How can I make my code do that?

$('button').on('click', function() {
  var timeOfCall = $('#timeOfCall').val(),
    timeOfResponse = $('#timeOfResponse').val(),
    hours = timeOfResponse.split(':')[0] - timeOfCall.split(':')[0],
    minutes = timeOfResponse.split(':')[1] - timeOfCall.split(':')[1];

  minutes = minutes.toString().length < 2 ? '0' + minutes : minutes;
  if (minutes < 0) {
    hours--;
    minutes = 60 + minutes;
  }
  hours = minutes.toString().length < 2 ? '0' + hours : hours;
  $('#delay').val(hours + ':' + minutes);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type="time" id="timeOfCall">
<input type="time" id="timeOfResponse">
<button>CLICK</button>
<input type="time" id="delay">
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • Welcome to StackOverflow. Please take the [tour](http://stackoverflow.com/tour) have a look around, and read through the [HELP center](http://stackoverflow.com/help), then read [How to Ask Question](http://stackoverflow.com/help/how-to-ask), [What types of questions should I avoid asking?](http://stackoverflow.com/help/dont-ask) and provide a [MCVE : Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). If people around can easily read and understand what you mean, or what the problem is, they'll be more likely willing to help :) – Dwhitz Apr 06 '18 at 08:49
  • 1
    There was a reason you were not allowed to add a JSFiddle link without code. I fixed your question for you. Please see the console for the message from jQuery – mplungjan Apr 06 '18 at 08:55
  • 1
    When using dates/times in code, ALWAYS convert them to a proper format (ie 24h / Date/Time/DateTime instances) and only go back to a string representation once you want to display them to the user, send them over the network, etc. – ThiefMaster Apr 06 '18 at 08:57
  • Can you explain why the 3rd box would be "00:45" (0hours 45mins) when the difference is an 1hour 45mins? – freedomn-m Apr 06 '18 at 09:00
  • yeah i m sorry in 3rd box time should be 01:45 – Shalu Tripathi Apr 06 '18 at 09:29
  • http://jsfiddle.net/uWT4r/136/ i dont know how do i show this code to you – Shalu Tripathi Apr 06 '18 at 09:32
  • Edit the question, then click "edit snippet" – mplungjan Apr 06 '18 at 09:55
  • You can find the answen on `https://stackoverflow.com/questions/30204172/jquery-how-to-get-the-difference-between-two-time-and-date-with-am-pm-format` – Parveen Singla Apr 06 '18 at 09:59
  • @MikeMcCaughan there is no date. And no need to create date objects – mplungjan Apr 06 '18 at 13:43
  • @mplungjan Ugh, another high rep protecting his answer to a dupe. How about [Check time difference in Javascript](https://stackoverflow.com/q/1787939)j? Or [Jquery time difference in hours from two fields](https://stackoverflow.com/q/11883768)? Or [Calculate Time Difference with JavaScript](https://stackoverflow.com/q/10804042)? – Heretic Monkey Apr 06 '18 at 13:48
  • @MikeMcCaughan I could not care less about "protecting my answer" - you assume too much here - I had a quick look for dupes and found the ones using date objects and decided to write one myself without a date object. If it makes you feel better I can delete my answer and hammerclose as dupe. – mplungjan Apr 06 '18 at 13:51
  • @mplungjan Why not post the answer on one of the duplicates then? Isn't that what the duplicates are for? And it's not what makes me feel better, it's about what's good for the site. Does having yet another duplicate of "calculate time difference in JavaScript" help the site? – Heretic Monkey Apr 06 '18 at 13:54
  • @MikeMcCaughan Why post on (old) links found if I do not think they identical - most of them have dates/timestamps and not just two times on the same date. I did not have time to investigate in deep details if my answer COULD be added to a 4 year old question and then close this. – mplungjan Apr 06 '18 at 13:56
  • @mplungjan I found [Calculate Time Difference with JavaScript](https://stackoverflow.com/q/10804042) within a minute of searching on the title. No dates involved. You're taking more time than that just arguing about this. It's baffling to me that people don't take an iota of time to search first before asking, or answering. [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/a/261593) – Heretic Monkey Apr 06 '18 at 14:02
  • @MikeMcCaughan The dupe you found within a minute does indeed seem identical in nature, but the answers are using date objects and assuming 24 hour time. It is baffling to me how unforgiving some users are here towards noobs. Yes, I can hammer close this question and add my answer to the others at the dupe you found. There are so many "calculate time diff" that one looking for "in am/pm" will never find the answer if this is deleted. But by all means. – mplungjan Apr 06 '18 at 14:10

0 Answers0