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

js 获取当前日期时间

作者:我在坚强也需要有人疼   发布日期:2025-09-13   浏览:55

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

// 使用 JavaScript 的 Date 对象来获取当前日期和时间
const now = new Date();

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

// 获取月份(注意:JavaScript 中的月份是从 0 开始的,所以需要加 1)
const month = String(now.getMonth() + 1).padStart(2, '0');

// 获取日期
const day = String(now.getDate()).padStart(2, '0');

// 获取小时
const hours = String(now.getHours()).padStart(2, '0');

// 获取分钟
const minutes = String(now.getMinutes()).padStart(2, '0');

// 获取秒
const seconds = String(now.getSeconds()).padStart(2, '0');

// 拼接成完整的日期时间字符串
const formattedDate = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;

console.log(formattedDate); // 输出格式如:2023-10-05 14:30:45

这段代码展示了如何使用 JavaScript 获取当前的日期和时间,并将其格式化为一个易读的字符串。

上一篇:js 字符串转时间

下一篇:js 获取前一天日期

大家都在看

js 数组对象排序

js 数组删掉第一个值

js fill

js 数组连接

js json数组

js 数组复制

js 复制数组

js 数组拷贝

js 对象数组合并

js 对象转数组

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

Laravel 中文站