在JavaScript中,可以使用Date对象来获取当前日期和时间。以下是获取当前日期和时间的代码示例:

var currentDate = new Date(); // 创建一个Date对象,表示当前日期和时间

var year = currentDate.getFullYear(); // 获取当前年份
var month = currentDate.getMonth() + 1; // 获取当前月份(注意月份是从0开始计数的,所以要加1)
var day = currentDate.getDate(); // 获取当前日期

var hours = currentDate.getHours(); // 获取当前小时
var minutes = currentDate.getMinutes(); // 获取当前分钟
var seconds = currentDate.getSeconds(); // 获取当前秒钟

console.log(year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds);

输出结果类似于:2022-1-1 12:34:56,表示当前日期是2022年1月1日,时间是12点34分56秒

js获取当天的时间

原文地址: http://www.cveoy.top/t/topic/iSJT 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录