Reformat string containing date with Javascript

前端 未结 3 1156
逝去的感伤
逝去的感伤 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:42

    one solution without regex:

    var expDate = '2016-03';
    var formatExp = expDate.split('-').reverse().join('/');
    //result is 03/2016
    alert('result: ' + formatExp);
    
    var formatExpShort = expDate.substring(2).split('-').reverse().join('/');
    //result is 03/16
    alert('result short: ' + formatExpShort);

提交回复
热议问题