How can we add 0 before single digit number of time format. Like if I have a time "0:3:25" (hh:mm:ss) format to convert into "00:03:25"
- 
                    You have to use a library like moment.js or use switch cases in javascript – Dev Man Jul 31 '19 at 06:01
 - 
                    5Possible duplicate of [Adding "0" if clock have one digit](https://stackoverflow.com/questions/12278272/adding-0-if-clock-have-one-digit) and or [How can I display time with leading zeros?](https://stackoverflow.com/questions/12230343/how-can-i-display-time-with-leading-zeros) – caramba Jul 31 '19 at 06:05
 
4 Answers
You shouldn't overcomplicate this:
const time = "0:3:25";
const paddedTime = time.split(':').map(e => `0${e}`.slice(-2)).join(':')
console.log(paddedTime)
split the string by semicolons (:), that yields an array (hours, minutes, seconds). map this array with a function that adds a 0 before every item in the array, and slice the last two digits (you get an array again). Then join the resulting array by semicolons (and you get a string).
Or you could use a regex instead of the split:
const time = "0:3:25";
const paddedTime = time.match(/\d+/g).map(e => `0${e}`.slice(-2)).join(':')
console.log(paddedTime)
The last part is the same with regex (map, slice, join).
And you also could use the padStart() (JavaScript built-in function):
const time = "0:3:25";
const paddedTime = time.split(':').map(e => e.padStart(2, 0)).join(':')
console.log(paddedTime)
padStart() on MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart
- 8,063
 - 2
 - 17
 - 34
 
You can do something like below (Explanations included):
const time = '0:3:25';
const correctedTime = parseTime(time);
console.log(correctedTime);
function parseTime(time){
  return time
    .split(':') // Split them into array as chunk of [hour, minute, second]
    .map(pad)   // Map them with `pad` function below
    .join(':'); // Join them back into 'hh:mm:ss'
}
  
function pad(n){
  return parseInt(n) < 10 // If number less than 10
    ? '0' + n             // Add '0' in front
    : n;                  // Else, return the original string.
}
- 6,898
 - 3
 - 19
 - 43
 
you can add split the existing time string on the ":", then for each portion, add the "0" then take the last two characters. Then simply join the portions back into a striung.
let time = "0:3:25";
function updateTime(){
  let newTimePortions = [];
  let timePortions = time.split(":");
    timePortions.forEach(function(portion,index) {
      newTimePortions[index] = ("0" + portion).slice(-2)
    })
   return newTimePortions.join(':')
}
console.log(updateTime()); // gives 00:03:25
- 15,194
 - 2
 - 25
 - 27
 
Please try below code
var dateinfo="0:3:25";
var newdate=dateinfo.split(":");
var hdate=newdate[0];
var mdate=newdate[1];
var sdate=newdate[2];
if(hdate.length == 1 ){
hdate="0"+hdate;
}
if(mdate.length == 1 ){
mdate="0"+mdate;
}
if(sdate.length == 1 ){
sdate="0"+sdate;
}
dateinfo=hdate+":"+mdate+":"+sdate;
This is work for me
- 1,280
 - 1
 - 9
 - 11