在Web开发中,经常需要获取当前的日期和时间,以便于在页面中显示或进行相应的操作。JavaScript提供了一些内置的方法,可以方便地获取当前的日期和时间。
var now = new Date();
var day = now.getDate();
var month = now.getMonth() + 1;
var year = now.getFullYear();
console.log("当前日期为:" + year + "-" + month + "-" + day);
| 获取当前时间
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
console.log("当前时间为:" + hours + ":" + minutes + ":" + seconds);
| 获取当前日期和时间
var now = new Date();
var day = now.getDate();
var month = now.getMonth() + 1;
var year = now.getFullYear();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
console.log("当前日期和时间为:" + year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds);
| 总结
原创文章,作者:guozi,如若转载,请注明出处:https://www.sudun.com/ask/89210.html