Created
January 31, 2014 11:56
-
-
Save umidjons/8730745 to your computer and use it in GitHub Desktop.
Left pad of string/number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Pads from left given subject with specified character. | |
* @param {string|number} number number or string to pad | |
* @param {number} width width of a subject | |
* @param {string|number} char character to pad | |
* @returns {string} padded subject | |
*/ | |
function pad( number, width, char ) | |
{ | |
char = char || '0'; | |
number = number + ''; | |
return number.length >= width ? number : new Array( width - number.length + 1 ).join( char ) + number; | |
} | |
var d = new Date(), | |
file_name = 'backup_' | |
+ d.getFullYear() | |
+ pad( d.getMonth() + 1, 2 ) | |
+ pad( d.getDate(), 2 ) | |
+ pad( d.getHours(), 2 ) | |
+ pad( d.getMinutes(), 2 ) | |
+ pad( d.getSeconds(), 2 ) + '.tar.gz'; | |
console.log('File name:', file_name); // File name: backup_20140130160905.tar.gz |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment