Javascript Number pad(size)

Description

Javascript Number pad(size)


Number.prototype.pad = function(size) {
  if (size==null) size=2;
  var s = String(this);
  while (s.length < size) {s = "0" + s;}
  return s;/*  ww  w.j a v  a  2  s . c o m*/
}

Javascript Number pad(size)

var net = require('net');
var port = process.argv[2];

Number.prototype.pad = function(size) {
    var s = String(this);
    while (s.length < (size || 2)) {s = "0" + s;}
    return s;//from   w ww  .j a  v  a 2  s.c o m
};

var server = net.createServer(function (socket) {
    var date = new Date();
    var year = date.getFullYear();
    var month = (date.getUTCMonth() + 1).pad(2);
    var day = (date.getDate()).pad(2);
    var hours = date.getHours();
    var minutes = date.getMinutes();
    socket.end(year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + "\n");
});
server.listen(port);

Javascript Number pad(size)

Number.prototype.pad = function(size){
    if(typeof(size) !== "number"){size = 2;}
    var s = String(this);
    while (s.length < size) s = "0" + s;
       return s;//w  ww. jav a2  s.  c o m
    }



PreviousNext

Related