Formatting time

Dev Question

So i have this data coming from a table which is a time 00:03:22.

how can i format this in js to the user to be like 3min 22 seconds ?
Alex LordAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Zakaria AcharkiAnalyst DeveloperCommented:
I suggest to you the use of MomentJS Library that allows you to simply Parse, validate, manipulate, and display dates and times in JavaScript.
ste5anSenior DeveloperCommented:
Just a comment: don't mix time with time spans. They are different data types.

Your given data looks like time, but a formatting in minutes and seconds denotes a time span. E.g. 11:35:22 vs. 695min 22sec.
Alex LordAuthor Commented:
okay so how do i do that ? moments is time span
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

ste5anSenior DeveloperCommented:
Hae? Time spans in MomentJS are called durations.
Zakaria AcharkiAnalyst DeveloperCommented:
If you don't want to use any extra library you could use the following function :

function humanizeTime(time) {
  var timeParts = time.split(':');
  var hours = Number(timeParts[0]);
  var minutes = Number(timeParts[1]);
  var seconds = Number(timeParts[2]);
  var huminizedTime = [];

  if (hours > 0) {
    huminizedTime.push(hours + (hours > 1 ? ' Hours' : ' Hour'));
  }
  if (minutes > 0) {
    huminizedTime.push(minutes + (minutes > 1 ? ' Minutes' : ' Minute'));
  }
  if (seconds > 0) {
    huminizedTime.push(seconds + (seconds > 1 ? ' Seconds' : ' Second'));
  }

  return huminizedTime.join(' ');
}

Open in new window

Working live sample

So just pass your time as a string like :

humanizeTime('00:03:22'); //3 Minutes 22 Seconds

Open in new window


NOTE : That will give you more flexibility to change the strings "Hour/Minute/Second" as in your question for example you want "3 min" instead of "3 Minutes" you could adjust it inside the function...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Julian HansenCommented:
Question
How do you want this to be displayed
12:00:22
Or this
12:23:00
Howard BashSenior Software EngineerCommented:
If you guarantee that the incoming format is hh:mm:ss then you could use the split function and build the string.  That would be the simpliest way ASSUMING the format is as stated.
ste5anSenior DeveloperCommented:
hmm, Howard's answer makes me think:

@Alex: Do you have a time, thus a Date, or a string?
Alex LordAuthor Commented:
it only a string this time
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.