Skip to content

Instantly share code, notes, and snippets.

@rinp
Last active August 29, 2015 14:21
Show Gist options
  • Save rinp/6df2297b7b54898101ae to your computer and use it in GitHub Desktop.
Save rinp/6df2297b7b54898101ae to your computer and use it in GitHub Desktop.
0埋め(JavaScript)
function (length) {
return function (str) {
var max = length - str.length;
// 利用する文字列長を考慮の上で初期値を決めておく。
var s = "0000";
while (s.length < max) {
s += "00";
}
return (s + str).slice(-length);
}
}
@rinp
Copy link
Author

rinp commented May 25, 2015

Array.prototype.join()の速度があまりよくないのであれば、ループで回す方がいいのかも。

利用箇所:年月日の0埋め

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment