js 获取当前时间特定格式

1.获取当前时间

let newDate = new Date(); // 当前日期和时间的 Date 对象

2.获取时间中的年月日时分秒

    let now = new Date();
    let year = now.getFullYear();            // 获取完整的年份(4位,1970-????)
                                             // 不推荐getYear(),因为它返回的是从1900年开始计算的年份,这可能导致混淆和错误。例如,如果年份是2023年,getYear()会返回123而不是2023。

    let month = now.getMonth() + 1;          // 注意:getMonth() 返回的月份是从0开始的
    let date = now.getDate();                // 获取当前日(1-31)
    let week = now.getDay();                 // 获取当前星期X(0-6,0代表星期天)
    let hours = now.getHours();              // 获取当前小时数(0-23)
    let minutes = now.getMinutes();          // 获取当前分钟数(0-59)
    let seconds = now.getSeconds();          // 获取当前秒数(0-59)
    let millisecond = now.getMilliseconds(); // 获取当前毫秒数(0-999)
    let timestamp = now.getTime();           // 时间戳(输出一个数字,表示从1970年1月1日 00:00:00 UTC以来的毫秒数)

    // now.toLocaleDateString();             // 获取当前日期
    // now.toLocaleTimeString();             // 获取当前时间
    // now.toLocaleString();                 // 获取日期与时间

    console.log(year, month, date, week, hours, minutes, seconds, millisecond);

3.时间的格式化

/**
 * 对Date的扩展,将 Date 转化为指定格式的String
 * 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符
 * 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
 * 例子:(new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2022-02-22 08:08:08.423
 *       (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2020-2-2 2:2:2.18
 **/

Date.prototype.Format = function (fmt) {
  // author: meizz
  var o = {
    "M+": this.getMonth() + 1, // 月份
    "d+": this.getDate(), // 日
    "h+": this.getHours(), // 小时
    "m+": this.getMinutes(), // 分
    "s+": this.getSeconds(), // 秒
    "q+": Math.floor((this.getMonth() + 3) / 3), // 季度
    S: this.getMilliseconds() // 毫秒
  };
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o) if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
  return fmt;
};
4.yyyy-MM-dd hh:mm:ss

yyyy-MM-dd hh:mm:ss 是一个常用于表示日期和时间的格式,但它有一个小问题:它混合了12小时制和24小时制的表示。

yyyy 表示四位数的年份,例如 2022。
MM 表示两位数的月份,例如 07 表示7月。
dd 表示两位数的日期,例如 17 表示17日。
hh 在这里可能是意图表示小时,但通常 hh 用于12小时制(即 01 到 12),而 HH 用于24小时制(即 00 到 23)。
mm 表示分钟,两位数,从 00 到 59。
ss 表示秒,两位数,从 00 到 59。

调用:


let time1 = new Date().Format("yyyy-MM-dd");
let time2 = new Date().Format("yyyy-MM-dd HH:mm:ss");// HH 24小时制 ,hh 12小时制
  • 4
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: 可以使用 JavaScript 中的 Date 对象来获取当前时间,并使用一些方法对其进行格式化。以下是一个示例代码: ```javascript var currentDate = new Date(); var year = currentDate.getFullYear(); var month = ("0" + (currentDate.getMonth() + 1)).slice(-2); var day = ("0" + currentDate.getDate()).slice(-2); var hour = ("0" + currentDate.getHours()).slice(-2); var minute = ("0" + currentDate.getMinutes()).slice(-2); var second = ("0" + currentDate.getSeconds()).slice(-2); var formattedDate = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second; console.log(formattedDate); ``` 该代码将输出格式为 `yyyy-mm-dd hh:mm:ss` 的当前日期和时间。 ### 回答2: 要使用JavaScript获取当前时间格式化,我们可以使用Date对象和一些内置的方法。 首先,我们创建一个Date对象来获取当前的日期和时间: ``` var currentDate = new Date(); ``` 接下来,我们可以使用Date对象的方法来获取特定的日期和时间组件,例如年、月、日、小时、分钟和秒: ``` var year = currentDate.getFullYear(); //获取当前年份 var month = currentDate.getMonth() + 1; //获取当前月份,注意月份从0开始计数,所以需要加1 var day = currentDate.getDate(); //获取当前日期 var hour = currentDate.getHours(); //获取当前小时 var minute = currentDate.getMinutes(); //获取当前分钟 var second = currentDate.getSeconds(); //获取当前秒数 ``` 现在,我们可以将这些日期和时间组件组合成我们想要的格式。例如,我们可以将它们组合成一个字符串形式的日期和时间,如"yyyy-mm-dd hh:mm:ss": ``` var formattedDate = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second; ``` 此时,我们就得到了格式化后的当前时间。 完整的代码示例如下: ``` var currentDate = new Date(); var year = currentDate.getFullYear(); var month = currentDate.getMonth() + 1; var day = currentDate.getDate(); var hour = currentDate.getHours(); var minute = currentDate.getMinutes(); var second = currentDate.getSeconds(); var formattedDate = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second; console.log(formattedDate); ``` 当我们运行这段代码时,会在控制台输出格式化后的当前时间。 ### 回答3: JavaScript获取当前时间格式化可以使用Date对象和一些日期方法来实现。 下面是一个获取当前日期时间的示例代码: ```javascript 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(); // 获取秒钟 // 可以根据需要添加格式化处理,比如添加前导零 month = month < 10 ? '0' + month : month; day = day < 10 ? '0' + day : day; hours = hours < 10 ? '0' + hours : hours; minutes = minutes < 10 ? '0' + minutes : minutes; seconds = seconds < 10 ? '0' + seconds : seconds; // 可以根据需要组合成不同的格式化字符串 var formattedDateTime = year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds; console.log(formattedDateTime); // 输出格式化后的当前时间 ``` 以上代码通过Date对象的一些方法获取到当前的年份、月份、日期、小时、分钟和秒钟。然后根据需要进行格式化处理,最后将格式化后的时间输出到控制台。 可以根据实际需求自定义格式化的字符串,如需要只获取日期部分,则可以只保留年月日部分,如果不需要前导零,则可以去除对应的格式化代码。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值