skip to Main Content

I have date string value in input text and I want to convert that text and display in ‘dd-MM-yyyy’ format in that input. How to convert it?

<input type="text" class="form-control" id="orderDateInput" value="11/15/2017 12:00:00 AM">

2

Answers


  1. You can use regular expressions:

    const regex = /(d{2})/(d{2})/(d{4})/;
    const value = '11/15/2017 12:00:00 AM'
    const newValue = value.replace(regex, (_, month, day, year) => {
      return `${day}/${month}/${year}`
    })
    console.log(newValue) // 15/11/2017 12:00:00 AM
    Login or Signup to reply.
  2. If the expected input is always in the form 11/15/2017 12:00:00 AM, i. e. with the date part written with / as separator, then the target string can be extracted like

    function toDateStr(s){
     const [m,d,y]=s.match(/d{1,2}/d{1,2}/d{4}/)[0].split("/");
     return [d,m,y].join("/");
    }
    
    
    console.log(["11/15/2017 12:00:00 AM","the date is 12/27/2019"].map(toDateStr))
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search