1

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

5
  • You might find this library useful: momentjs.com Commented Nov 27, 2013 at 8:49
  • @Neel he already stated "Please spare me of any anti-regex comments". Commented Nov 27, 2013 at 8: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! Commented Nov 27, 2013 at 8: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 Commented Nov 27, 2013 at 9: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. Commented Nov 27, 2013 at 9:15

3 Answers 3

2

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('-'));
Sign up to request clarification or add additional context in comments.

1 Comment

That is nice, I'll try it!
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

1 Comment

Good idea, I'd probably implement it as a prototype on Number or String
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

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.