Javascript Secs to Time


/ Published in: JavaScript
Save to your folder(s)

Convert number of seconds to HH:MM


Copy this code and paste it in your HTML
  1. function secsTime(inSecs) {
  2. var timeStr = new String();
  3. var seconds = parseInt(inSecs);
  4. if (seconds < 0) {
  5. timeStr = "-";
  6. seconds *= -1;
  7. }
  8. var hours = parseInt(seconds/3600);
  9. var minutes = parseInt((seconds - (hours * 3600))/60);
  10. if (hours < 10) {
  11. if (hours == 0) {
  12. timeStr += "00:";
  13. } else {
  14. timeStr += "0" + hours + ":";
  15. }
  16. } else {
  17. timeStr += hours + ":";
  18. }
  19. if (minutes < 10) {
  20. if (minutes == 0) {
  21. timeStr += "00";
  22. } else {
  23. timeStr += "0" + minutes;
  24. }
  25. } else {
  26. timeStr += minutes;
  27. }
  28. return timeStr;
  29. };

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.