0

How to change the time depending on value in javascript if it has colon, remove the colon and should be integer format if it has no colon, add the colon in javascript

function checkColon(str) {
  return str.replace(/:/g, '');

}
var t1 = "10:40"
var t2 = "01:40"
var t3 = "0240"
var t4 = "1250"
console.log(this.checkColon(t1), this.checkColon(t2), this.checkColon(t3), this.checkColon(t4));

Expected Output:

1040
0140
02:40
12:50
1
  • I made you a snippet. Your numbers need to be strings so I wrapped them in " Commented Sep 4, 2020 at 8:00

6 Answers 6

2

You could use replace by regex group for the case when string does not have colon

function checkColon(str) {
  if (str.includes(':')) {
    return str.replace(/:/g, '')
  } else {
    return str.replace(/(\d{2})(\d{2})/g, '$1:$2')
  }
}

function checkColon(str) {
  if (str.includes(':')) {
    return str.replace(/:/g, '')
  } else {
    return str.replace(/(\d{2})(\d{2})/g, '$1:$2')
  }
}

var t1 = '10:40'
var t2 = '01:40'
var t3 = '0240'
var t4 = '1250'

console.log(checkColon(t1), checkColon(t2), checkColon(t3), checkColon(t4))

Sign up to request clarification or add additional context in comments.

Comments

0

Hope this works for you

function checkColon(str){
  if (str.indexOf(':') > -1) {
    return str.replace(/:/g,'');
  } else {
    return [str.slice(0, 2), ':', str.slice(2)].join('');
  }

}
var t1= '10:40';
var t2= '01:40';
var t3= '0240';
var t4= '1250';
console.log(checkColon(t1), checkColon(t2), checkColon(t3), checkColon(t4));

Comments

0

Using replacer method

Regex Demo

function checkColon(str) {
  return str.replace(/(?<=\d\d)(:)?(?=\d\d)/g, (m, p) => p ? '' : ':');
}

var t1 = "10:40"
var t2 = "01:40"
var t3 = "0240"
var t4 = "1250"

console.log(this.checkColon(t1), this.checkColon(t2), this.checkColon(t3), this.checkColon(t4));

Comments

0

Inside checkColon check if the string contains : if so , then replace it or else add it using substr

function checkColon(str) {
  if (str.indexOf(':') !== -1) {
    return str.replace(/:/g, '');
  } else {
    return `${str.substr(0,1)}:${str.substr(2,str.length)}`
  }


}
var t1 = "10:40"
var t2 = "01:40"
var t3 = "0240"
var t4 = "1250"
console.log(this.checkColon(t1), this.checkColon(t2), this.checkColon(t3), this.checkColon(t4));

Comments

0

Check with includes if colon is in string. Delete it with replace or add it to the with substr partetd string.

function checkColon(str) {
  return (str.includes(':')) ? str.replace(/:/g, '') : str.substr(0,2) + ':' +  str.substr(2);

}
var t1 = "10:40"
var t2 = "01:40"
var t3 = "0240"
var t4 = "1250"
console.log(this.checkColon(t1), this.checkColon(t2), this.checkColon(t3), this.checkColon(t4));

Comments

0

Not sure I understand the usecase but try this

const checkColon =  str => {
   if (str.includes(":")) return str.replace(/:/g, '');
   let arr = str.split(""); 
   arr.splice(2,0,":");
   return arr.join("");
};   

const t1 = "10:40";
const t2 = "01:40";
const t3 = "0240";
const t4 = "1250";
console.log(checkColon(t1), checkColon(t2), checkColon(t3), checkColon(t4));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.