js中获取当前系统的时间,格式2020-10-30 14:05:38

js中实现获取当前的具体时间

function getCurrentTime() {
    let nowTime = new Date(),
		year = nowTime.getFullYear(),
		month = nowTime.getMonth() + 1 >= 10 ? nowTime.getMonth() + 1 : '0' + (nowTime.getMonth() + 1),
		day = nowTime.getDate() >= 10 ? nowTime.getDate() : '0' + nowTime.getDate(),
		hours = nowTime.getHours() >= 10 ? nowTime.getHours() : '0' + nowTime.getHours(),
		minute = nowTime.getMinutes() >= 10 ? nowTime.getMinutes() : '0' + nowTime.getMinutes(),
		second = nowTime.getSeconds() >= 10 ? nowTime.getSeconds() : '0' + nowTime.getSeconds(),
		currentTime = "";
	currentTime = year + '-' + month + '-' + day + " " + hours + ":" + minute + ":" + second;
	return currentTime;
}  
getCurrentTime(); //"2020-10-30 14:05:38"

利用js简单却快速的实现这个小小的功能。如果是打印每一秒的时间呢,那我们不妨给上面的方法添加一个调取的定时器:

function getCurrentTime() {
    let nowTime = new Date(),
		year = nowTime.getFullYear(),
		month = nowTime.getMonth() + 1 >= 10 ? nowTime.getMonth() + 1 : '0' + (nowTime.getMonth() + 1),
		day = nowTime.getDate() >= 10 ? nowTime.getDate() : '0' + nowTime.getDate(),
		hours = nowTime.getHours() >= 10 ? nowTime.getHours() : '0' + nowTime.getHours(),
		minute = nowTime.getMinutes() >= 10 ? nowTime.getMinutes() : '0' + nowTime.getMinutes(),
		second = nowTime.getSeconds() >= 10 ? nowTime.getSeconds() : '0' + nowTime.getSeconds(),
		currentTime = "";
	currentTime = year + '-' + month + '-' + day + " " + hours + ":" + minute + ":" + second;
    console.log(currentTime);
}
  
var time = setInterval(function(){
	getCurrentTime();
},1000); 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尔嵘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值