convert dd/mm/yyyy to mm/dd/yyyy in javascript

前端 未结 12 1082
深忆病人
深忆病人 2020-12-15 18:49

I want to convert dd/mm/yyyy to mm/dd/yyyy in javascript.

12条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-15 19:54

    Try this example where a regular expression is used to find and switch the parts of the string:

    var d1 = "03/25/2011";
    var d2 = d1.replace(/^(\d{1,2}\/)(\d{1,2}\/)(\d{4})$/,"$2$1$3");
    alert("d1:"+d1 +"\n d2:"+d2 )
    
    /* 
    // OUTPUT
    
      d1: 03/25/2011
      d2: 25/03/2011
    
    */
    

    EDIT:

    I noticed that no other answer is using regular expression and I really wonder why... :)

提交回复
热议问题