Formatting of ISO date time

I’m looking to change the format of my date and time stamps on a scroll plugin.

The code is currently;

{ return ("Date: " + d.getDate() + "/" + d.getMonth()+1) + "/" + d.getYear().toString().substr(-2) + " - Time " + d.getHours() + ":" + d.getMinutes() }

which shows

[Date: 11/11/12 - Time: 10:0 - 12:00]

I want it to show:

[Date: 11/11/12 - Time: 10:00 - 12:00]

Why is the second zero missing???

Not sure what I’m doing wrong - the plugin script that deals with it is;


      date_formatter: function(d, allday_p) {
        if (allday_p) {
          return "" + (d.getFullYear()) + "-" + (pad_zero(d.getMonth() + 1)) + "-" + (pad_zero(d.getDate()));
        } else {
          return "" + (d.getFullYear()) + "-" + (pad_zero(d.getMonth() + 1)) + "-" + (pad_zero(d.getDate())) + " " + (pad_zero(d.getHours())) + ":" + (pad_zero(d.getMinutes()));
        }
      },
      daterange_formatter: function(sd, ed, allday_p) {
        if (allday_p) {
          if (sd.getDate() !== ed.getDate() || sd.getMonth() !== ed.getMonth()) {
            return "" + (this.date_formatter(sd, allday_p)) + " - " + (pad_zero(ed.getMonth() + 1)) + "-" + (pad_zero(ed.getDate()));
          } else {
            return this.date_formatter(sd, allday_p);
          }
        } else {
          if (sd.getDate() !== ed.getDate() || sd.getMonth() !== ed.getMonth()) {
            return "" + (this.date_formatter(sd, allday_p)) + " - " + (pad_zero(ed.getMonth() + 1)) + "-" + (pad_zero(ed.getDate())) + " " + (pad_zero(ed.getHours())) + ":" + (pad_zero(ed.getMinutes()));
          } else if (sd.getHours() !== ed.getHours() || sd.getMinutes() !== ed.getMinutes()) {
            return "" + (this.date_formatter(sd, allday_p)) + " - " + (pad_zero(ed.getHours())) + ":" + (pad_zero(ed.getMinutes()));
          } else {
            return this.date_formatter(sd, allday_p);
          }
        }
      }
    };

You’re needing to add a leading zero on to those values.

A simple solution to that would be something like:


function leadingZero(value) {
    return (value < 10) ? '0' + value : value;
}

{
    return "Date: " +
        leadingZero(d.getDate()) + "/" + leadingZero(d.getMonth() + 1) + "/" + d.getYear().toString().substr(-2) +
        " - Time " +
        leadingZero(d.getHours()) + ":" + leadingZero(d.getMinutes());
}