How to calculate the time difference between 2 date time values












5















I am trying to calculate the time difference between 2 date time strings.



I have 2 inputs where the input string is something like this "1:00 PM" and the second one "3:15 PM". I want to know the time difference. So for the above example I want to display 3.15



What I have done:




  1. Converted the time to a 24 hours format. So "1:00 PM" becomes "13:00:00"

  2. Appended the new time to a date like so: new Date("1970-1-1 13:00:00")

  3. Calculated the difference like so:


Code:



var total = Math.round(((new Date("1970-1-1 " + end_time) - 
new Date("1970-1-1 " + start_time) ) / 1000 / 3600) , 2 )


But the total is always returning integers and not decimals, so the difference between "1:00 PM" and "3:15 PM" is 2 not 2.15.



I have also tried this (using jQuery, but that is irrelevant):



$('#to_ad,#from_ad').change(function(){
$('#total_ad').val( getDiffTime() );
});

function fixTimeString(time){
var hours = Number(time.match(/^(d+)/)[1]);
var minutes = Number(time.match(/:(d+)/)[1]);
var AMPM = time.match(/s(.*)$/)[1];
if(AMPM == "PM" && hours<12) hours = hours+12;
if(AMPM == "AM" && hours==12) hours = hours-12;
var sHours = hours.toString();
var sMinutes = minutes.toString();
if(hours<10) sHours = "0" + sHours;
if(minutes<10) sMinutes = "0" + sMinutes;
return sHours + ':' + sMinutes + ':00';
}

function getDiffTime(){
var start_time = fixTimeString($('#from_ad').val());
var end_time = fixTimeString($('#to_ad').val());
var start = new Date("1970-1-1 " + end_time).getTime(),
end = new Date("1970-1-1 " + start_time).getTime();
return parseInt(((start - end) / 1000 / 3600, 10)*100) / 100;
}


But the total_ad input is displaying only integer values.



How can I fix this problem?










share|improve this question

























  • possible duplicate of Comparing 2 times with jquery

    – isherwood
    Jan 12 '14 at 21:16











  • 3.15 - 1 = 2.15 ?

    – adeneo
    Jan 12 '14 at 21:22






  • 1





    Where does jQuery comes into the picture? Isn't it just plain JavaScript?

    – biziclop
    Jan 12 '14 at 21:22
















5















I am trying to calculate the time difference between 2 date time strings.



I have 2 inputs where the input string is something like this "1:00 PM" and the second one "3:15 PM". I want to know the time difference. So for the above example I want to display 3.15



What I have done:




  1. Converted the time to a 24 hours format. So "1:00 PM" becomes "13:00:00"

  2. Appended the new time to a date like so: new Date("1970-1-1 13:00:00")

  3. Calculated the difference like so:


Code:



var total = Math.round(((new Date("1970-1-1 " + end_time) - 
new Date("1970-1-1 " + start_time) ) / 1000 / 3600) , 2 )


But the total is always returning integers and not decimals, so the difference between "1:00 PM" and "3:15 PM" is 2 not 2.15.



I have also tried this (using jQuery, but that is irrelevant):



$('#to_ad,#from_ad').change(function(){
$('#total_ad').val( getDiffTime() );
});

function fixTimeString(time){
var hours = Number(time.match(/^(d+)/)[1]);
var minutes = Number(time.match(/:(d+)/)[1]);
var AMPM = time.match(/s(.*)$/)[1];
if(AMPM == "PM" && hours<12) hours = hours+12;
if(AMPM == "AM" && hours==12) hours = hours-12;
var sHours = hours.toString();
var sMinutes = minutes.toString();
if(hours<10) sHours = "0" + sHours;
if(minutes<10) sMinutes = "0" + sMinutes;
return sHours + ':' + sMinutes + ':00';
}

function getDiffTime(){
var start_time = fixTimeString($('#from_ad').val());
var end_time = fixTimeString($('#to_ad').val());
var start = new Date("1970-1-1 " + end_time).getTime(),
end = new Date("1970-1-1 " + start_time).getTime();
return parseInt(((start - end) / 1000 / 3600, 10)*100) / 100;
}


But the total_ad input is displaying only integer values.



How can I fix this problem?










share|improve this question

























  • possible duplicate of Comparing 2 times with jquery

    – isherwood
    Jan 12 '14 at 21:16











  • 3.15 - 1 = 2.15 ?

    – adeneo
    Jan 12 '14 at 21:22






  • 1





    Where does jQuery comes into the picture? Isn't it just plain JavaScript?

    – biziclop
    Jan 12 '14 at 21:22














5












5








5


1






I am trying to calculate the time difference between 2 date time strings.



I have 2 inputs where the input string is something like this "1:00 PM" and the second one "3:15 PM". I want to know the time difference. So for the above example I want to display 3.15



What I have done:




  1. Converted the time to a 24 hours format. So "1:00 PM" becomes "13:00:00"

  2. Appended the new time to a date like so: new Date("1970-1-1 13:00:00")

  3. Calculated the difference like so:


Code:



var total = Math.round(((new Date("1970-1-1 " + end_time) - 
new Date("1970-1-1 " + start_time) ) / 1000 / 3600) , 2 )


But the total is always returning integers and not decimals, so the difference between "1:00 PM" and "3:15 PM" is 2 not 2.15.



I have also tried this (using jQuery, but that is irrelevant):



$('#to_ad,#from_ad').change(function(){
$('#total_ad').val( getDiffTime() );
});

function fixTimeString(time){
var hours = Number(time.match(/^(d+)/)[1]);
var minutes = Number(time.match(/:(d+)/)[1]);
var AMPM = time.match(/s(.*)$/)[1];
if(AMPM == "PM" && hours<12) hours = hours+12;
if(AMPM == "AM" && hours==12) hours = hours-12;
var sHours = hours.toString();
var sMinutes = minutes.toString();
if(hours<10) sHours = "0" + sHours;
if(minutes<10) sMinutes = "0" + sMinutes;
return sHours + ':' + sMinutes + ':00';
}

function getDiffTime(){
var start_time = fixTimeString($('#from_ad').val());
var end_time = fixTimeString($('#to_ad').val());
var start = new Date("1970-1-1 " + end_time).getTime(),
end = new Date("1970-1-1 " + start_time).getTime();
return parseInt(((start - end) / 1000 / 3600, 10)*100) / 100;
}


But the total_ad input is displaying only integer values.



How can I fix this problem?










share|improve this question
















I am trying to calculate the time difference between 2 date time strings.



I have 2 inputs where the input string is something like this "1:00 PM" and the second one "3:15 PM". I want to know the time difference. So for the above example I want to display 3.15



What I have done:




  1. Converted the time to a 24 hours format. So "1:00 PM" becomes "13:00:00"

  2. Appended the new time to a date like so: new Date("1970-1-1 13:00:00")

  3. Calculated the difference like so:


Code:



var total = Math.round(((new Date("1970-1-1 " + end_time) - 
new Date("1970-1-1 " + start_time) ) / 1000 / 3600) , 2 )


But the total is always returning integers and not decimals, so the difference between "1:00 PM" and "3:15 PM" is 2 not 2.15.



I have also tried this (using jQuery, but that is irrelevant):



$('#to_ad,#from_ad').change(function(){
$('#total_ad').val( getDiffTime() );
});

function fixTimeString(time){
var hours = Number(time.match(/^(d+)/)[1]);
var minutes = Number(time.match(/:(d+)/)[1]);
var AMPM = time.match(/s(.*)$/)[1];
if(AMPM == "PM" && hours<12) hours = hours+12;
if(AMPM == "AM" && hours==12) hours = hours-12;
var sHours = hours.toString();
var sMinutes = minutes.toString();
if(hours<10) sHours = "0" + sHours;
if(minutes<10) sMinutes = "0" + sMinutes;
return sHours + ':' + sMinutes + ':00';
}

function getDiffTime(){
var start_time = fixTimeString($('#from_ad').val());
var end_time = fixTimeString($('#to_ad').val());
var start = new Date("1970-1-1 " + end_time).getTime(),
end = new Date("1970-1-1 " + start_time).getTime();
return parseInt(((start - end) / 1000 / 3600, 10)*100) / 100;
}


But the total_ad input is displaying only integer values.



How can I fix this problem?







date time






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jun 23 '18 at 9:30









trincot

122k1587120




122k1587120










asked Jan 12 '14 at 21:11









MikeMike

87763260




87763260













  • possible duplicate of Comparing 2 times with jquery

    – isherwood
    Jan 12 '14 at 21:16











  • 3.15 - 1 = 2.15 ?

    – adeneo
    Jan 12 '14 at 21:22






  • 1





    Where does jQuery comes into the picture? Isn't it just plain JavaScript?

    – biziclop
    Jan 12 '14 at 21:22



















  • possible duplicate of Comparing 2 times with jquery

    – isherwood
    Jan 12 '14 at 21:16











  • 3.15 - 1 = 2.15 ?

    – adeneo
    Jan 12 '14 at 21:22






  • 1





    Where does jQuery comes into the picture? Isn't it just plain JavaScript?

    – biziclop
    Jan 12 '14 at 21:22

















possible duplicate of Comparing 2 times with jquery

– isherwood
Jan 12 '14 at 21:16





possible duplicate of Comparing 2 times with jquery

– isherwood
Jan 12 '14 at 21:16













3.15 - 1 = 2.15 ?

– adeneo
Jan 12 '14 at 21:22





3.15 - 1 = 2.15 ?

– adeneo
Jan 12 '14 at 21:22




1




1





Where does jQuery comes into the picture? Isn't it just plain JavaScript?

– biziclop
Jan 12 '14 at 21:22





Where does jQuery comes into the picture? Isn't it just plain JavaScript?

– biziclop
Jan 12 '14 at 21:22












2 Answers
2






active

oldest

votes


















0














Math.round rounds to the nearest integer, multiply and divide instead



var start = new Date("1970-1-1 " + start_time).getTime(),
end = new Date("1970-1-1 " + end_time).getTime();

var total = (parseInt(((start-end) / 1000 / 3600)*100, 10)) / 100;


FIDDLE



When you take the time 15:15:00 and subtract 13:00:00, you're left with 2.15 hours, not 3.15, and this example would return 2.15 even without making sure there is only two decimals, but for other times that might not be the case.

You could also use toFixed(2), but that would leave you with 3.00 and not 3 etc.






share|improve this answer


























  • this is not working for me for some reason. Please check my question and I added the code that I am working with. Thanks

    – Mike
    Jan 12 '14 at 21:42











  • what format is "start_time" and "end_time" before you use them? What format is "total" after you initialize it? Is it a string or just a number of milliseconds? Please give more detail.

    – BrianLegg
    Dec 18 '15 at 16:58











  • @BrianLegg - Lets assume the OP no longer has issues with this code, it's been two years ?

    – adeneo
    Dec 18 '15 at 19:54











  • I may not be the OP, but I'm trying to do the same thing and wanted more detail. It's fine though, I found the answer I was looking for. Thanks

    – BrianLegg
    Dec 18 '15 at 20:18



















0














This is how I calculate it:



    calculateDiff();

function calculateDiff(){
_start = "7:00 AM";
_end = "1:00 PM";

_start_time = parseAMDate(_start);
_end_time = parseAMDate(_end);

if (_end_time < _start_time){
_end_time = parseAMDate(_end,1);
}

var difference= _end_time - _start_time;

var hours = Math.floor(difference / 36e5),
minutes = Math.floor(difference % 36e5 / 60000);
if (parseInt(hours) >= 0 ){
if (minutes == 0){
minutes = "00";
}
alert(hours+":"+minutes);
}
}

function parseAMDate(input, next_day) {

var dateReg = /(d{1,2}):(d{2})s*(AM|PM)/;

var hour, minute, result = dateReg.exec(input);

if (result) {
hour = +result[1];
minute = +result[2];

if (result[3] === 'PM' && hour !== 12) {
hour += 12;
}
}
if (!next_day) {
return new Date(1970, 01, 01, hour, minute).getTime();
}else{
return new Date(1970, 01, 02, hour, minute).getTime();
}
}





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f21080336%2fhow-to-calculate-the-time-difference-between-2-date-time-values%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Math.round rounds to the nearest integer, multiply and divide instead



    var start = new Date("1970-1-1 " + start_time).getTime(),
    end = new Date("1970-1-1 " + end_time).getTime();

    var total = (parseInt(((start-end) / 1000 / 3600)*100, 10)) / 100;


    FIDDLE



    When you take the time 15:15:00 and subtract 13:00:00, you're left with 2.15 hours, not 3.15, and this example would return 2.15 even without making sure there is only two decimals, but for other times that might not be the case.

    You could also use toFixed(2), but that would leave you with 3.00 and not 3 etc.






    share|improve this answer


























    • this is not working for me for some reason. Please check my question and I added the code that I am working with. Thanks

      – Mike
      Jan 12 '14 at 21:42











    • what format is "start_time" and "end_time" before you use them? What format is "total" after you initialize it? Is it a string or just a number of milliseconds? Please give more detail.

      – BrianLegg
      Dec 18 '15 at 16:58











    • @BrianLegg - Lets assume the OP no longer has issues with this code, it's been two years ?

      – adeneo
      Dec 18 '15 at 19:54











    • I may not be the OP, but I'm trying to do the same thing and wanted more detail. It's fine though, I found the answer I was looking for. Thanks

      – BrianLegg
      Dec 18 '15 at 20:18
















    0














    Math.round rounds to the nearest integer, multiply and divide instead



    var start = new Date("1970-1-1 " + start_time).getTime(),
    end = new Date("1970-1-1 " + end_time).getTime();

    var total = (parseInt(((start-end) / 1000 / 3600)*100, 10)) / 100;


    FIDDLE



    When you take the time 15:15:00 and subtract 13:00:00, you're left with 2.15 hours, not 3.15, and this example would return 2.15 even without making sure there is only two decimals, but for other times that might not be the case.

    You could also use toFixed(2), but that would leave you with 3.00 and not 3 etc.






    share|improve this answer


























    • this is not working for me for some reason. Please check my question and I added the code that I am working with. Thanks

      – Mike
      Jan 12 '14 at 21:42











    • what format is "start_time" and "end_time" before you use them? What format is "total" after you initialize it? Is it a string or just a number of milliseconds? Please give more detail.

      – BrianLegg
      Dec 18 '15 at 16:58











    • @BrianLegg - Lets assume the OP no longer has issues with this code, it's been two years ?

      – adeneo
      Dec 18 '15 at 19:54











    • I may not be the OP, but I'm trying to do the same thing and wanted more detail. It's fine though, I found the answer I was looking for. Thanks

      – BrianLegg
      Dec 18 '15 at 20:18














    0












    0








    0







    Math.round rounds to the nearest integer, multiply and divide instead



    var start = new Date("1970-1-1 " + start_time).getTime(),
    end = new Date("1970-1-1 " + end_time).getTime();

    var total = (parseInt(((start-end) / 1000 / 3600)*100, 10)) / 100;


    FIDDLE



    When you take the time 15:15:00 and subtract 13:00:00, you're left with 2.15 hours, not 3.15, and this example would return 2.15 even without making sure there is only two decimals, but for other times that might not be the case.

    You could also use toFixed(2), but that would leave you with 3.00 and not 3 etc.






    share|improve this answer















    Math.round rounds to the nearest integer, multiply and divide instead



    var start = new Date("1970-1-1 " + start_time).getTime(),
    end = new Date("1970-1-1 " + end_time).getTime();

    var total = (parseInt(((start-end) / 1000 / 3600)*100, 10)) / 100;


    FIDDLE



    When you take the time 15:15:00 and subtract 13:00:00, you're left with 2.15 hours, not 3.15, and this example would return 2.15 even without making sure there is only two decimals, but for other times that might not be the case.

    You could also use toFixed(2), but that would leave you with 3.00 and not 3 etc.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 12 '14 at 21:26

























    answered Jan 12 '14 at 21:15









    adeneoadeneo

    262k19279309




    262k19279309













    • this is not working for me for some reason. Please check my question and I added the code that I am working with. Thanks

      – Mike
      Jan 12 '14 at 21:42











    • what format is "start_time" and "end_time" before you use them? What format is "total" after you initialize it? Is it a string or just a number of milliseconds? Please give more detail.

      – BrianLegg
      Dec 18 '15 at 16:58











    • @BrianLegg - Lets assume the OP no longer has issues with this code, it's been two years ?

      – adeneo
      Dec 18 '15 at 19:54











    • I may not be the OP, but I'm trying to do the same thing and wanted more detail. It's fine though, I found the answer I was looking for. Thanks

      – BrianLegg
      Dec 18 '15 at 20:18



















    • this is not working for me for some reason. Please check my question and I added the code that I am working with. Thanks

      – Mike
      Jan 12 '14 at 21:42











    • what format is "start_time" and "end_time" before you use them? What format is "total" after you initialize it? Is it a string or just a number of milliseconds? Please give more detail.

      – BrianLegg
      Dec 18 '15 at 16:58











    • @BrianLegg - Lets assume the OP no longer has issues with this code, it's been two years ?

      – adeneo
      Dec 18 '15 at 19:54











    • I may not be the OP, but I'm trying to do the same thing and wanted more detail. It's fine though, I found the answer I was looking for. Thanks

      – BrianLegg
      Dec 18 '15 at 20:18

















    this is not working for me for some reason. Please check my question and I added the code that I am working with. Thanks

    – Mike
    Jan 12 '14 at 21:42





    this is not working for me for some reason. Please check my question and I added the code that I am working with. Thanks

    – Mike
    Jan 12 '14 at 21:42













    what format is "start_time" and "end_time" before you use them? What format is "total" after you initialize it? Is it a string or just a number of milliseconds? Please give more detail.

    – BrianLegg
    Dec 18 '15 at 16:58





    what format is "start_time" and "end_time" before you use them? What format is "total" after you initialize it? Is it a string or just a number of milliseconds? Please give more detail.

    – BrianLegg
    Dec 18 '15 at 16:58













    @BrianLegg - Lets assume the OP no longer has issues with this code, it's been two years ?

    – adeneo
    Dec 18 '15 at 19:54





    @BrianLegg - Lets assume the OP no longer has issues with this code, it's been two years ?

    – adeneo
    Dec 18 '15 at 19:54













    I may not be the OP, but I'm trying to do the same thing and wanted more detail. It's fine though, I found the answer I was looking for. Thanks

    – BrianLegg
    Dec 18 '15 at 20:18





    I may not be the OP, but I'm trying to do the same thing and wanted more detail. It's fine though, I found the answer I was looking for. Thanks

    – BrianLegg
    Dec 18 '15 at 20:18













    0














    This is how I calculate it:



        calculateDiff();

    function calculateDiff(){
    _start = "7:00 AM";
    _end = "1:00 PM";

    _start_time = parseAMDate(_start);
    _end_time = parseAMDate(_end);

    if (_end_time < _start_time){
    _end_time = parseAMDate(_end,1);
    }

    var difference= _end_time - _start_time;

    var hours = Math.floor(difference / 36e5),
    minutes = Math.floor(difference % 36e5 / 60000);
    if (parseInt(hours) >= 0 ){
    if (minutes == 0){
    minutes = "00";
    }
    alert(hours+":"+minutes);
    }
    }

    function parseAMDate(input, next_day) {

    var dateReg = /(d{1,2}):(d{2})s*(AM|PM)/;

    var hour, minute, result = dateReg.exec(input);

    if (result) {
    hour = +result[1];
    minute = +result[2];

    if (result[3] === 'PM' && hour !== 12) {
    hour += 12;
    }
    }
    if (!next_day) {
    return new Date(1970, 01, 01, hour, minute).getTime();
    }else{
    return new Date(1970, 01, 02, hour, minute).getTime();
    }
    }





    share|improve this answer






























      0














      This is how I calculate it:



          calculateDiff();

      function calculateDiff(){
      _start = "7:00 AM";
      _end = "1:00 PM";

      _start_time = parseAMDate(_start);
      _end_time = parseAMDate(_end);

      if (_end_time < _start_time){
      _end_time = parseAMDate(_end,1);
      }

      var difference= _end_time - _start_time;

      var hours = Math.floor(difference / 36e5),
      minutes = Math.floor(difference % 36e5 / 60000);
      if (parseInt(hours) >= 0 ){
      if (minutes == 0){
      minutes = "00";
      }
      alert(hours+":"+minutes);
      }
      }

      function parseAMDate(input, next_day) {

      var dateReg = /(d{1,2}):(d{2})s*(AM|PM)/;

      var hour, minute, result = dateReg.exec(input);

      if (result) {
      hour = +result[1];
      minute = +result[2];

      if (result[3] === 'PM' && hour !== 12) {
      hour += 12;
      }
      }
      if (!next_day) {
      return new Date(1970, 01, 01, hour, minute).getTime();
      }else{
      return new Date(1970, 01, 02, hour, minute).getTime();
      }
      }





      share|improve this answer




























        0












        0








        0







        This is how I calculate it:



            calculateDiff();

        function calculateDiff(){
        _start = "7:00 AM";
        _end = "1:00 PM";

        _start_time = parseAMDate(_start);
        _end_time = parseAMDate(_end);

        if (_end_time < _start_time){
        _end_time = parseAMDate(_end,1);
        }

        var difference= _end_time - _start_time;

        var hours = Math.floor(difference / 36e5),
        minutes = Math.floor(difference % 36e5 / 60000);
        if (parseInt(hours) >= 0 ){
        if (minutes == 0){
        minutes = "00";
        }
        alert(hours+":"+minutes);
        }
        }

        function parseAMDate(input, next_day) {

        var dateReg = /(d{1,2}):(d{2})s*(AM|PM)/;

        var hour, minute, result = dateReg.exec(input);

        if (result) {
        hour = +result[1];
        minute = +result[2];

        if (result[3] === 'PM' && hour !== 12) {
        hour += 12;
        }
        }
        if (!next_day) {
        return new Date(1970, 01, 01, hour, minute).getTime();
        }else{
        return new Date(1970, 01, 02, hour, minute).getTime();
        }
        }





        share|improve this answer















        This is how I calculate it:



            calculateDiff();

        function calculateDiff(){
        _start = "7:00 AM";
        _end = "1:00 PM";

        _start_time = parseAMDate(_start);
        _end_time = parseAMDate(_end);

        if (_end_time < _start_time){
        _end_time = parseAMDate(_end,1);
        }

        var difference= _end_time - _start_time;

        var hours = Math.floor(difference / 36e5),
        minutes = Math.floor(difference % 36e5 / 60000);
        if (parseInt(hours) >= 0 ){
        if (minutes == 0){
        minutes = "00";
        }
        alert(hours+":"+minutes);
        }
        }

        function parseAMDate(input, next_day) {

        var dateReg = /(d{1,2}):(d{2})s*(AM|PM)/;

        var hour, minute, result = dateReg.exec(input);

        if (result) {
        hour = +result[1];
        minute = +result[2];

        if (result[3] === 'PM' && hour !== 12) {
        hour += 12;
        }
        }
        if (!next_day) {
        return new Date(1970, 01, 01, hour, minute).getTime();
        }else{
        return new Date(1970, 01, 02, hour, minute).getTime();
        }
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jul 29 '16 at 10:54









        Seth

        1,37711125




        1,37711125










        answered Mar 31 '15 at 8:56









        Andrews KalinovskiAndrews Kalinovski

        262




        262






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f21080336%2fhow-to-calculate-the-time-difference-between-2-date-time-values%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            Refactoring coordinates for Minecraft Pi buildings written in Python