So I'm rewriting dates in javacript and as familiar js spits dates like 2013-1-1 that isn't very useful always. Instead I'm looking for a routine that will form this date to the correct iso-version 2013-01-01
Today I make this by using string
var b = new Date('2013-1-1');
var result = b.getFullYear() + "-" +
(b.getMonth().toString().length == 1 ? "0" + parseInt(b.getMonth() + 1) : parseInt(b.getMonth() + 1)) + "-" +
(b.getDate().toString().length == 1 ? "0" + b.getDate() : b.getDate());
This works but it is ugly. Is there a better way to perform this using RegEx?
Please spare me of any anti-regex comments
if startsWith('0')), so the actual formatting task will still be problematic. I love regexes, but if your code already works, use it. If you must change your code, your other option is to get a javascript sprintf implementation.getMonthand.getDateparts of your code - which isn't going to be any cleaner. You're not going to be able to use a simple regex replace for your purposes.