skip to Main Content

I’m getting data in the form of date and time:

Monday Jan 23 10:14:27 2024

How in Javascript using .replace to remove everything except time without seconds:

10:14

.

Please tell me, because I am already lost in these regular expressions.
Thank you all.

2

Answers


  1. Use String.prototype.match

    const reg = /d{2}:d{2}/
    const str = 'Monday Jan 23 10:14:27 2024'
    const time = str.match(reg)[0]
    console.log(time)
    Login or Signup to reply.
  2. You could simply convert this string to date and extract the required information from it.

    const theDate = new Date('Monday Jan 23 10:14:27 2024');
    const displayValue = `${theDate.getHours()}:${theDate.getMinutes()}`;
    
    console.log(displayValue); // 10:14
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search