1.标题 获取当前日期函数,不足补0
function currTimeFormate() {
var date = new Date(); // 当前日期时间
var year = date.getFullYear(); // 年份
var month = ('0'+(date.getMonth()+1)).slice(-2); // 月份+1
var day = ('0'+date.getDate()).slice(-2); // 日
var hour = ('0'+date.getHours()).slice(-2); // 小时
var minute = ('0'+date.getMinutes()).slice(-2); // 分钟
var second = ('0'+date.getSeconds()).slice(-2); // 秒数
var millisecond = ('000'+date.getMilliseconds()).slice(-3); // 毫秒数0-999
console.log(year + '-' + month + '-' + day + ' ' +
hour + ':' + minute + ':' + second + '.' + millisecond);
//# sourceURL=snippet:///Script%20snippet%20%233
}
2.控制台呈现结果:
2022-07-13 15:34:13.780
3.使用padStart():
对于toString()后想要不足两位的 -前- 面补0
var a = 2;
a.toString().padStart(2,'0') //表示补足两位且补的内容为0
4.使用padEnd():
和3相反使用,后面补字符