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

js 年月日时分秒

作者:ら非你不取丶   发布日期:2025-04-20   浏览:51

// 获取当前的年月日时分秒
function getDateTime() {
    const now = new Date(); // 创建一个表示当前日期和时间的Date对象

    const year = now.getFullYear(); // 获取年份
    const month = String(now.getMonth() + 1).padStart(2, '0'); // 获取月份(从0开始,所以要加1),并确保两位数
    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'); // 获取秒数,并确保两位数

    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`; // 返回格式化的日期时间字符串
}

console.log(getDateTime()); // 输出类似 "2023-10-05 14:30:45" 的字符串

上一篇:js new date() 年月日

下一篇:js 获取当月第一天

大家都在看

js 数组对象排序

js 数组删掉第一个值

js fill

js 数组复制

js 复制数组

js 数组拷贝

js 对象转数组

js 深拷贝数组

js 获取今天年月日

js jsonp

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

Laravel 中文站