Padding in JavaScript

When working with numbers in JavaScript, it can be desirable to pad numbers before displaying them, for example, in a report or email.

Example

A company would like an email containing times regarding a certain call including the start and finish time.
 
They have a template that they would like followed, including spacing and styling. The customer specifically requires that the time is formatted in two-digit groups.
 
Call Report

Request Logged:    09:37:05
Request Completed: 09:39:57
 
Having read the Getting Time and Dates in JavaScript article, you may write your timestamp like this:
 
var d = new Date(); // Create Date object

[Timestamp] = d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds(); // 9:37:5
 
As you can see, the output isn't as specified, however, padding can be used.
 
A simple padding function can be created, please see the example below:
 
function pad(number) {
    if (number <= 9) {
        return '0' + number; // If the number is under or equal to 9 (any single-digit number), return the number with an extra 0
    } else {
        return '' + number;  // If the number is over 9 (double-digit number), do not alter it.
    }
}

var d = new Date();          // Create Date() object.

// Call the pad() function and pass in the current hour, minutes and seconds, returning the timestamp which is parsed correctly.
[Timestamp] = pad(d.getHours()) + ':' + pad(d.getMinutes()) + ':' + pad(d.getSeconds()); // 09:37:05
Having used the padding function, the date will now be formatted in two-digit groups, ready for use in the email that the company would like created.