Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

javascript 获取当前时间

作者:血之メ狂霸   发布日期:2026-02-05   浏览:125

// 获取当前时间的示例代码

// 使用 new Date() 创建一个表示当前日期和时间的 Date 对象
const now = new Date();

// 获取当前的年份
const year = now.getFullYear();

// 获取当前的月份(注意:月份是从 0 开始的,所以需要加 1)
const month = now.getMonth() + 1;

// 获取当前的日期
const date = now.getDate();

// 获取当前的小时数(24 小时制)
const hours = now.getHours();

// 获取当前的分钟数
const minutes = now.getMinutes();

// 获取当前的秒数
const seconds = now.getSeconds();

// 格式化输出当前时间
const formattedTime = `${year}-${String(month).padStart(2, '0')}-${String(date).padStart(2, '0')} ${String(hours).padStart(2, '0')}:${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`;

console.log(formattedTime); // 输出格式化后的时间,例如:2023-10-05 14:30:45

上一篇:javascript的基本数据类型

下一篇:javascript typeof

大家都在看

js 数组打乱顺序

js 数组对象排序

js 对象数组排序

js 数组删掉第一个值

js fill

js 数组连接

js json数组

js 数组复制

js 复制数组

js 数组拷贝

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站