Reformat string containing date with Javascript

前端 未结 3 1160
逝去的感伤
逝去的感伤 2020-11-29 13:44

I have the following string which I ultimately need to have in the format of mm/yy

    var expDate = 2016-03;
    var formatExp = expDate.replace(/-/g , \"/\         


        
3条回答
  •  无人及你
    2020-11-29 14:28

    Do you really need to use RegExp?

    Why not creating a simple function that splits the exp Date and returns it the way you want it?

    function parseDate(expDate){
    
         var dateArray = expDate.split('-')
         return dateArray[1] + '/' + dateArray[0].substring(2,4)
    }
    

    The split functions creates an array, the element in position 1 is the month, the element in position 2 is the year, on the latter you apply the substring function which extrapolates the last two digits.

提交回复
热议问题