2

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 , "/");

This gets me to 2016/03, but how can i get to 03/16?

3 Answers 3

2

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);

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks and that's super close but it does not drop the first two digits of the year?
You the man! Thanks a ton. I'll mark as resolved in just a min
1

With a RegExp :

'2016-03'.replace(/^\d{2}(\d{2})-(\d{2})$/, '$1/$2')

1 Comment

this got a vote from me, i love regex but im too slow for that kind of solution ;)
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.