获取日期

function 输出当前日期和时间() {
    const now = new Date();

    const year = now.getFullYear(); // 年
    const month = now.getMonth() + 1; // 月份 (0-11, 加1得到1-12)
    const day = now.getDate(); // 日期
    const hour = now.getHours(); // 小时 (0-23)
    const minute = now.getMinutes(); // 分钟
    const second = now.getSeconds(); // 秒

    // 格式化日期和时间为中文格式字符串
    const dateTimeString = `${year}年${month}月${day}日 ${hour}时${minute}分${second}秒`;

    // 输出格式化的日期和时间
    console.log(dateTimeString);
}

  2024年2月28日 15时24分36秒

posted @ 2024-02-28 16:16  小林222  阅读(31)  评论(0)    收藏  举报