当前位置:   article > 正文

在JavaScript中获取当前时间yyyymmddhhmmss的六种实现方式_js获取当前时间

js获取当前时间

在编写JavaScript代码时,遇到需要获取当前日期和时间并将其格式化为 yyyymmddhhmmss 字符串的情况。可以使用本文中介绍的几种实现方式中的任意一种。

方法一:使用Date对象

使用 Date 对象来获取当前日期和时间。

示例代码:

  1. const now = new Date();
  2. const year = now.getFullYear();
  3. const month = ('0' + (now.getMonth() + 1)).slice(-2);
  4. const day = ('0' + now.getDate()).slice(-2);
  5. const hours = ('0' + now.getHours()).slice(-2);
  6. const minutes = ('0' + now.getMinutes()).slice(-2);
  7. const seconds = ('0' + now.getSeconds()).slice(-2);
  8. const formattedTime = year + month + day + hours + minutes + seconds;
  9. console.log(formattedTime);

结果截图:

代码说明:

在上面的代码中,使用了 getFullYear、getMonth、getDate、getHours、getMinutes 和 getSeconds 函数来获取年、月、日、小时、分钟和秒。然后,使用 slice 函数将所有这些值转换为两位数字并将它们连接到一个字符串中。

方法二:使用moment.js

Moment.js是一个流行的JavaScript日期库,它提供了许多日期和时间操作方法。

示例代码:

  1. const moment = require('moment');
  2. const formattedTime = moment().format('YYYYMMDDHHmmss');
  3. console.log(formattedTime);

结果截图:

无,笔者使用时没有运行成功

代码说明:

在上面的代码中,使用了moment.js库的format函数将当前时间格式化为 yyyymmddhhmmss 的字符串。

方法三:使用Intl.DateTimeFormat

Intl.DateTimeFormat是一个内置的JavaScript日期库,它提供了本地化和格式化日期的方法。

示例代码:

  1. const options = { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false };
  2. const formattedTime = new Intl.DateTimeFormat('en-US', options).format(new Date()).replace(/[^0-9]/g, '');
  3. console.log(formattedTime);

结果截图:

代码说明:

在上面的代码中,使用了Intl.DateTimeFormat来格式化当前时间,并使用正则表达式将所有非数字字符替换为空字符串,以生成 yyyymmddhhmmss 的字符串。

方法四:使用day.js

day.js是一个轻量级的JavaScript日期库,它提供了许多日期和时间操作方法。

示例代码:

  1. const dayjs = require('dayjs');
  2. const formattedTime = dayjs().format('YYYYMMDDHHmmss');
  3. console.log(formattedTime);

结果截图:

无,笔者使用时没有运行成功

代码说明:

在上面的代码中,使用了day.js库的format函数将当前时间格式化为 yyyymmddhhmmss 的字符串。

方法五:使用toLocaleString

在JavaScript中,可以使用 toLocaleString 函数来获取本地化的日期和时间。

示例代码:

  1. const now = new Date();
  2. const formattedTime = now.toLocaleString('en-US', {year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit'}).replace(/[^\d]/g, '');
  3. console.log(formattedTime);

结果截图:

代码说明:

在上面的代码中,使用了 toLocaleString 函数获取本地化的日期和时间,并使用正则表达式将所有非数字字符替换为空字符串,以生成 yyyymmddhhmmss 的字符串。

方法六:使用String.prototype.padStart

在JavaScript中,我们可以使用 padStart 函数来将数字字符串填充到指定的长度。

示例代码:

  1. const now = new Date();
  2. const year = now.getFullYear();
  3. const month = String(now.getMonth() + 1).padStart(2, '0');
  4. const day = String(now.getDate()).padStart(2, '0');
  5. const hours = String(now.getHours()).padStart(2, '0');
  6. const minutes = String(now.getMinutes()).padStart(2, '0');
  7. const seconds = String(now.getSeconds()).padStart(2, '0');
  8. const formattedTime = year + month + day + hours + minutes + seconds;
  9. console.log(formattedTime);

结果截图:

代码说明:

在上面的代码中,使用 padStart 函数将所有数字字符串填充到两位,并将它们连接到一个字符串中,以生成 yyyymmddhhmmss 的字符串。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/631437
推荐阅读
相关标签
  

闽ICP备14008679号