-4

I have two times which are in 12 hours format. Example 10:00 am and 3:30 pm. I want to show 330 minutes between them. I have tried many ways but couldn't get accurate result.

my script:

    time1= 'yy/mm/dd 10:00 am';
    time2='yy/mm/dd 3:30 pm';
    from1 = new Date(time1);
    to1 = new Date(time2);
    console.log(from1-to1);
  • 2
    _"I have tried many ways"_ Please add what you tried to your question so that we can a) see what you tried and that you at least made an effort and b) don't duplicate your efforts – j08691 Oct 18 '17 at 16:09
  • Theres no *time* datatype in js. What is it really? Give examples of in and output. Show what youve tried. – Jonas Wilms Oct 18 '17 at 16:11
  • I mean in javascript. I want to extract minutes difference between two times which are in 12 hours format contains am and pm. – Venkat Akula Oct 18 '17 at 16:16
  • Sorry, but there's just so much wrong in the code that it's hard to know where to begin. You'll have to look up how to properly convert dates and times to a Date object. – JJJ Oct 18 '17 at 16:19
  • i can get hours difference by using getHours() as var starttime= new Date("18/10/2017 10:00 am").getHours(); var endtime = new Date("18/10/2017 3:30 pm").getHours(); var hoursdiff = starttime- endtime; But I'm trying to achive how many minutes between two times. – Venkat Akula Oct 18 '17 at 16:25
  • Using `getHours()` only works if both dates are in the same day. Take a look at https://stackoverflow.com/questions/7709803/javascript-get-minutes-between-two-dates - and please [edit] the question to add any information (such as code), it's much better and more readable than putting in the comments. –  Oct 18 '17 at 16:50
  • Yes i need the minute difference for same date, that's why i tried getHours(). – Venkat Akula Oct 18 '17 at 16:54

2 Answers2

0

I would isolate the hours part of your time and use 24hr time to make things easier.

var start_time ="1000";
var end_time ="1530";

var start_hour = start_time.slice(0, -2);
var start_minutes = start_time.slice(-2);

var end_hour = end_time.slice(0, -2);
var end_minutes = end_time.slice(-2);

var startDate = new Date(0,0,0, start_hour, start_minutes);
var endDate = new Date(0,0,0, end_hour, end_minutes);

var millis = endDate - startDate;
var minutes = millis/1000/60;

console.log(minutes);
kemotoe
  • 1,730
  • 13
  • 27
  • ok but how to get start_time 1000 and end_time 1530. I can get minutes by time = new Date(); minutes =time.getMinutes(); – Venkat Akula Oct 18 '17 at 16:39
  • From your code you are just appending strings onto the date objects. Do you know those times or did you just add them to try to explain what you are doing? – kemotoe Oct 18 '17 at 16:43
  • actually i'm converting date to 12 hours format and kept hidden in a button, when we clicking on that button i'm getting that as a string and placing it. So here to explain clearly i'm attaching it directly as 10:00 am – Venkat Akula Oct 18 '17 at 16:45
  • Since you are getting the strings you can perform the function above with those strings – kemotoe Oct 18 '17 at 16:49
  • i can get 2 times in 12 hour format from select dropdown that contains am/pm so i need to tell how many minutes difference between those two times not dates. – Venkat Akula Oct 18 '17 at 16:52
0

I don't usually like writing code for people, but I'm feeling nice today.

function parse12hToMin(timeStr){ //returns the minutes as an offset from 12:00 AM
  let match12h = new RegExp(
    "^"              + // start of string
    "(1[0-2]|[1-9])" + // hour
    ":"              + // separator
    "([0-5][0-9])"   + // minutes
    " "              + // separator
    "(am|pm)"          // AM or PM
  , 'i');              // case insensitive

  let matched = timeStr.match(match12h);
  let min = parseInt(matched[1]) * 60                      // hours
          + parseInt(matched[2])                           // minutes
          + (matched[3].toLowerCase() === "pm" ? 720 : 0); // 720 min PM offset
  return min;
}

function minutesDiff12h(start, end){
  return parse12hToMin(end) - parse12hToMin(start);
}

console.assert(minutesDiff12h("10:00 am","3:30 pm") === 330);

Please always try to list what you tried, and show us the code snippets that aren't working.