0

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

Eric Herlitz
  • 25,354
  • 27
  • 113
  • 157
  • You might find this library useful: [momentjs.com](http://momentjs.com/) – Tafari Nov 27 '13 at 08:49
  • @Neel he already stated "Please spare me of any anti-regex comments". – Tafari Nov 27 '13 at 08:51
  • @Tafari, moment looks awesome even if it is a bit over the top to implement for my purposes. I'll have a look at the source and see! – Eric Herlitz Nov 27 '13 at 08:55
  • *"Is there a better way to perform this using RegEx?"* Probably not, no. You already have access to the actual data; extracting it with regex will just introduce possible errors, plus you don't have conditionals in regexes (i.e. `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](http://stackoverflow.com/questions/610406/javascript-equivalent-to-printf-string-format) – beerbajay Nov 27 '13 at 09:09
  • You could only use regex to replace the `.getMonth` and `.getDate` parts 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. – OGHaza Nov 27 '13 at 09:15

3 Answers3

1

A non-regex solution would be a generic padding function. First get your date in the non-padded version then you can split on the separator and pad it as necessary. Something like this:

var date = '2013-1-1';

var pad = function(n) {
  return function(str) {
    while (str.length < n) {
      str = '0'+ str;
    }
    return str;
  }
};

date = date.split(/-/g).map(pad(2)).join('-'); //=> 2013-01-01
elclanrs
  • 92,861
  • 21
  • 134
  • 171
1

may be this could help:

var str="2013-1-1";
var m = str.match(/^(\d{4})-(\d{1})-(\d{1})$/);
console.log([m[1], "0".concat([2]-1), "0".concat(m[3])].join('-'));
Vipul Paralikar
  • 1,508
  • 10
  • 22
0

based on elclanrs suggestion I wrote an extension method

// Add 0 to single numbers
Number.prototype.padDate = function () {
    // Add +1 if input is 0 (js months starts at 0)
    var number = this == 0 ? 1 : this;
    return number.toString().length == 1 ? "0" + number : number;
};

This allows me to build dates like this

var b = new Date('2013-1-1');
var result = b.getFullYear() + "-" + b.getMonth().padDate() + "-" + b.getDate().padDate();

Much cleaner, thanks

Eric Herlitz
  • 25,354
  • 27
  • 113
  • 157