Javascript Date toHyphenedString()

Description

Javascript Date toHyphenedString()



Date.prototype.toHyphenedString = function() {
    var date = this;
    var lpad = function(value, padding, len) {
        var text = value.toString();
        while(text.length < len) {
            text = padding + text;// w  w w  .  j  a  v  a  2  s . co  m
        }
        return text;
    };

    var year = date.getFullYear().toString();
    var month = lpad((date.getMonth()+1).toString(), '0', 2);
    var day = lpad(date.getDate().toString(), '0', 2);
    return [year, month, day].join("-");
}



PreviousNext

Related