我在网上发现了这个 Javascript 脚本,它每秒刷新一次时间并显示它。我希望程序显示带前导零的数字。目前它显示 6/8/2017 - 19:8:54,但我希望它显示 06/08/2017 - 19:08:54。
<script type="text/javascript">
function display_c(){
var refresh=1000; // Refresh rate in milli seconds
mytime =setTimeout('display_ct()',refresh)
}
function display_ct() {
var strcount
var x = new Date()
var x1=x.getDate() + "/" + x.getMonth() + "/" + x.getYear();
x1 = x1 + " - " + x.getHours( )+ ":" + x.getMinutes() + ":" +
x.getSeconds();
document.getElementById('ct').innerHTML = x1;
tt=display_c();
}
</script>
最佳答案
您可以使用以下内容:
var x1 = ('0' + x.getDate()).slice(-2) + '/'
+ ('0' + (x.getMonth()+1)).slice(-2) + '/'
+ x.getFullYear() + '-'
+ ('0' + x.getHours()).slice(-2) + ':'
+ ('0' + x.getMinutes()).slice(-2) + ':'
+ ('0' + x.getSeconds()).slice(-2);
关于javascript - 日期和时间中带前导零的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43124631/