扫码关注官方订阅号
我在做一个获取时间的项目;当时间是个位数时候加个0;请问这个判断怎么写
光阴似箭催人老,日月如移越少年。
let sec = new Date().getSeconds().toString() sec = sec.length < 2 '0' + sec : sec console.log(sec)
自己实现 String.prototype.padLeft
String.prototype.padLeft
String.prototype.padLeft = function(len, char) { return this.length < len ? char + this : this } // 如果字符串长度不足两位数,则在字符串前面补上字符 '0' console.log(new Date().getSeconds().toString().padLeft(2, '0'))
ES6 String.prototype.padStarthttps://developer.mozilla.org...
String.prototype.padStart
// 如果字符串长度不足两位数,则在字符串前面补上字符 '0' console.log(new Date().getSeconds().toString().padStart(2, '0'))
顺便安利一下,日期时间类最好的库: Momentjs中文网:http://momentjs.cn/
Momentjs
判断是否<10就行了
length判断。
if(parseInt(time) < 10) {
time = '0'+time;
}
function twoSpace(s){
var s=""+s; if(s.length==1){ s="0"+s; } return s;
if (strDate >= 0 && strDate <= 9) { strDate = "0" + strDate; }
function test(a){ a = +a return parseInt(a / 10) + '' + a % 10 }
我想你是想格式化输出日期时间吧?用以下库可以比较优雅http://blog.stevenlevithan.co...
dateFormat(now, "yyyy-mm-dd h:MM:ss");
function toTwo(n){
return n<10?"0"+n:""+n;
在你想要用这个函数的地方加上就可以啦
var str=''+ num; str.length==2?return true:return false; function fun(){ num>10?return true:return false;} num<99?return false:return fun();
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
自己实现
String.prototype.padLeftES6
String.prototype.padStarthttps://developer.mozilla.org...
顺便安利一下,日期时间类最好的库:
Momentjs中文网:http://momentjs.cn/
判断是否<10就行了
length判断。
if(parseInt(time) < 10)
{
}
function twoSpace(s){
}
我想你是想格式化输出日期时间吧?用以下库可以比较优雅
http://blog.stevenlevithan.co...
function toTwo(n){
}
在你想要用这个函数的地方加上就可以啦