Convert minute to time for display I am new to javaScript, I just coded a method where I can convert a minute to a time in hh:mm AP/PM. I was then displaying the time in a table.  I was wondering if there is a better approach to this? Because my code seems quite messy, with many if and else statement.

function convertMinsToHrsMin(minutes) {
                var str = '';
                var h = Math.floor(minutes / 60);
                var m = minutes - (h * 60);
                h = h &lt; 10 ? '0' + h : h;
                m = m &lt; 10 ? '0' + m : m;
                if (h &gt;= 12) {
                    if (h == 12) {
                        str = "PM";
                    }
                    else if (h &gt;= 22) {
                        h = (h - 12);
                        str = "PM";
                    }
                    else {
                        h = '0' + (h - 12);
                        str = "PM";
                    }
                }
                else {
                    if (h == 0) {
                        h = 12;
                        str = "AM";
                    }
                    else {
                        str = "AM";
                    }                     
                }                          
                return h + ":" + m + " " + str;
            }

            startTime = convertMinsToHrsMin(720);
            endTime = convertMinsToHrsMin(1080); 

            alert(startTime);
            alert(endTime);


Here is a jsFiddle to work on: https://jsfiddle.net/Issaki1/txns3mhc/
I was wondering if there is a better approach to this?


Yes. Use a library. Using what's already done is always better than doing it yourself. ;)

The next best thing you could do is convert minutes into milliseconds and instantiate a Date object from it. From there, JS does most of the heavy lifting for you. Here's an example (not extensively tested, but you get the idea):



const pad = n =&gt; `0${n}`.slice(-2)

const convertMinsToHrsMin = mins =&gt; {
  const date = new Date(mins * 60 * 1000)
  
  // Need to be UTC since timestamps are universal
  // but normal time methods operate based on local time.
  const hours = date.getUTCHours()
  const minutes = date.getUTCMinutes()
  
  // If 0, as in 00:00 or 12:00, default to 12.
  const hours12 = hours % 12 || 12
  const period = hours &gt;= 12 ? 'PM' : 'AM'
  
  return `${pad(hours12)}:${pad(minutes)}${period}`
}

startTime = convertMinsToHrsMin(720);
endTime = convertMinsToHrsMin(1080);

alert(startTime);
alert(endTime);




As for your code:


Use console.log instead of alert. I know alert is a quick way to visualize values, but getting the habit of using console.log will do wonders for your skin.
Avoid very deep and branchy conditionals. if statements don't do anything on its own and has to be mutating some variable. That means you define variables up top. There is a chance that you might miss a branch and leave that variable empty or contain an unwanted value.
Use a ternary for short conditionals, like determining AM and PM. It's short, and gets you into not mutating variables.
Use template literals to construct strings. This way, you don't need to break out strings and you always get a string (not a NaN should your expression fail you due to coercion).
And lastly, get familiar with the native APIs. They do lots of heavy lifting for you so you don't have to write it yourself.