RAJNIK PATEL
RAJNIK PATEL

Reputation: 1049

While converting number of seconds to time format got wrong answer

When I convert number of seconds 165 to time format like 02:45 I got wrong answer 1 hour plus in hour part 3:45. I don't know where is actual problem is.

console.log(ConvertStringToTimeFormat(165));

function ConvertStringToTimeFormat(timeString) {

        var timeFormat = "";

        if (timeString != null && timeString != null != "") {

            if (Math.round((parseInt(timeString) / 60)) > 0)
                timeFormat += Math.round((parseInt(timeString) / 60)) + ":";
            else
                timeFormat += "00:";

            if ((parseInt(timeString) % 60) > 0)
                timeFormat += (parseInt(timeString) % 60);
            else
                timeFormat += "00";
        }
        else {
            timeFormat = "00:00";
        }

        return timeFormat;
    }

And also when number of seconds is 125 then I got 2:5 as answer but I need 02:05

Thanks in advance.

Upvotes: 1

Views: 77

Answers (7)

Adriano Spadoni
Adriano Spadoni

Reputation: 4790

I rewrote your code using skinning a bit:

const ConvertStringToTimeFormat = (minutes) => ("00" + Math.floor(minutes / 60)).slice(-2) +":"+ ("00" + (minutes % 60)).slice(-2)

console.log(ConvertStringToTimeFormat(165))

Upvotes: 1

shizhz
shizhz

Reputation: 12501

You need Math.floor rather than Math.round, the later returns The value of the given number rounded to the nearest integer.

And for the format problem, try the following code:

console.log(ConvertStringToTimeFormat(125));

function formatHourOrMinute(hom) {
  return ("0" + hom).slice( - 2);
}

function ConvertStringToTimeFormat(timeString) {
        if (timeString) {
            var timeInt = parseInt(timeString);
            var hour = Math.floor(timeInt / 60);
            var minute = timeInt % 60;
            return formatHourOrMinute(hour) + ":" + formatHourOrMinute(minute);
            
        } else {
            return "00:00";
        }
    }

Upvotes: 1

Sagar V
Sagar V

Reputation: 12478

Use Math.floor instead of Math.round because 165/60 in Math.round is 3 but you want 2. So, Make use of Math.floor to find previous integer value.

console.log(ConvertStringToTimeFormat(165));

function ConvertStringToTimeFormat(timeString) {

        var timeFormat = "";

        if (timeString != null && timeString != null && timeString!= "") {
            if (Math.round((parseInt(timeString) / 60)) > 0)
                timeFormat += Math.floor((parseInt(timeString) / 60)) + ":";
            else
                timeFormat += "00:";

            if ((parseInt(timeString) % 60) > 0)
                timeFormat += (parseInt(timeString) % 60);
            else
                timeFormat += "00";
        }
        else {
            timeFormat = "00:00";
        }

        return timeFormat;
    }

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386654

You could use an array for the parts and get the padded strings and join the result with :.

function convert(min) {
    return [Math.floor(min / 60), min % 60].map(function (a) { return ('00' + a).slice(-2); }).join(':');
}

console.log(convert(165));

ES6

function convert(min) {
    return [Math.floor(min / 60), min % 60].map(a => ('00' + a).slice(-2)).join(':');
}

console.log(convert(165));

Upvotes: 2

James Donnelly
James Donnelly

Reputation: 128791

Where you're using Math.round(), it's rounding up 2.75 to 3. You should use Math.floor() instead.

console.log(ConvertStringToTimeFormat(165));

function ConvertStringToTimeFormat(timeString) {

        var timeFormat = "";

        if (timeString != null && timeString != null != "") {

            if (Math.floor((parseInt(timeString) / 60)) > 0)
                timeFormat += Math.floor((parseInt(timeString) / 60)) + ":";
            else
                timeFormat += "00:";

            if ((parseInt(timeString) % 60) > 0)
                timeFormat += (parseInt(timeString) % 60);
            else
                timeFormat += "00";
        }
        else {
            timeFormat = "00:00";
        }

        return timeFormat;
    }

Upvotes: 1

Kaique Garcia
Kaique Garcia

Reputation: 528

As you're using Math.round function, you should know that your value will be rounded to up if the decimals are greater than or equal to 5. Let's simulate it:

165/60 = 2,75

It will round to up: 3!

Use Math.floor instead and your problem's solved.

console.log(ConvertStringToTimeFormat(165));

function ConvertStringToTimeFormat(timeString) {

        var timeFormat = "";

        if (timeString != null && timeString != null != "") {

            if (Math.floor((parseInt(timeString) / 60)) > 0)
                timeFormat += Math.floor((parseInt(timeString) / 60)) + ":";
            else
                timeFormat += "00:";

            if ((parseInt(timeString) % 60) > 0)
                timeFormat += (parseInt(timeString) % 60);
            else
                timeFormat += "00";
        }
        else {
            timeFormat = "00:00";
        }

        return timeFormat;
    }

Upvotes: 1

mehulmpt
mehulmpt

Reputation: 16587

Try this code. You have a lot of unnecessary code there:

function minutesToHours(min) {
   var minutes = min % 60;
   var hours = (min-minutes)/60;
   minutes = minutes < 10 ? '0' + minutes : minutes;
   hours = hours < 10 ? '0' + hours : hours;
   return hours+':'+minutes;
}

console.log(minutesToHours(165));

Upvotes: 0

Related Questions