skip to Main Content

I have this String

var data = "Mon 08:30am - 04:30pm"

and I need it in this form

//expected String remove am pm and convert pm timing into 24 hrs time format
Result = 8:30-16:30

How Can I do it with Regex in Javascript .I tried it using substring but thats not the correct way.

I tried it with this but didnt find this correct way

var amHourFrom = parseInt(details[0].data.substring(4, 2)));    
var amMinuteFrom = parseInt(details[0].data.substring(7,2)));   
var pmHourFrom = parseInt(details[0].data.substring(14, 2)));   //04
var pmMinuteFrom = parseInt(details[0].data.substring(17,2)));  //30

if(details[0].data.substring(19,2)=="pm"){
{pmHourFrom = hourFrom +12;     //04+12 = 16    
}
var Str= amHourFrom + ":" + amMinuteFrom "-" + pmHourFrom+ ":" + pmMinuteFrom;
var result = Str.replace(/^(?:00:)?0?/, '');

I want to do it with some other way

4

Answers


    1. As days will always be three characters in length, you can simply slice the string from index 4 (space after the day name).
    2. Split the string with -.
    3. Then simply convert the time by passing time one by one and then join them with -.

    Here is the demo-

    var data = "Mon 08:30am - 04:30pm"
    var time = data.slice(4).replace(/ /g, '').split('-');
    
    const convertTime = timeStr => {
       const time = timeStr.match(/d+:d+/g).join("")
       const modifier = timeStr.match(/[a-z]+/gi).join("");
       let [hours, minutes] = time.split(':');
       if (hours === '12') {
          hours = '00';
       }
       if (modifier.toLowerCase() === 'pm') {
          hours = parseInt(hours, 10) + 12;
       }
       return `${hours}:${minutes}`;
    };
    
    let value1 = convertTime(time[0]) + " - " + convertTime(time[1])
    console.log(value1)

    If the string is dynamic, please, let me know.

    Login or Signup to reply.
  1. We can try the following regex replacement approach:

    var data = "Mon 08:30am - 04:30pm";
    var times = data.replace(/(d{2}):(d{2})([ap]m)/g, (m, x, y, z) => z === "am" ? x + ":" + y : "" + (12 + parseInt(x)) + ":" + y)
                    .match(/d{2}:d{2}/g)
                    .join("-");
    console.log(times);

    The first chained call to replace() converts the hours component to 24 hour time by adding 12 hours in the case of pm times. Then we find all time matches, and join together by dash in the final string output.

    Login or Signup to reply.
  2. Assuming you want to stick with your current implementation (which works, just has various bugs within the code), here is a working version:

        var data = "Mon 08:30am - 04:30pm";
    
        var amHourFrom = parseInt(data.substring(4, 6));    
        var amMinuteFrom = parseInt(data.substring(7,9));   
        var pmHourFrom = parseInt(data.substring(14, 16));  
        var pmMinuteFrom = parseInt(data.substring(17,19));  
    
        if(data.substring(19,21)=="pm"){
            pmHourFrom += 12;
        }
        var str= amHourFrom + ":" + amMinuteFrom + "-" + pmHourFrom+ ":" + pmMinuteFrom;
    
    Login or Signup to reply.
  3. I have no easy and short solution for the addition of 12 at the moment but to convert the data with Regex you can use this

    var result = data.replace(/[^0-9:-]/g, "");
    

    This replaces all characters with nothing except the letters in brackets.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search