js获取当前时间(年月日时分秒)

 1.获取当前时间


// 获取当前时间
    getNowTime() {
      const yy = new Date().getFullYear()
      const MM = (new Date().getMonth() + 1) < 10 ? '0' + (new Date().getMonth() + 1) : (new Date().getMonth() + 1)
      const dd = new Date().getDate() < 10 ? '0' + new Date().getDate() : new Date().getDate()
      const HH = new Date().getHours() < 10 ? '0' + new Date().getHours() : new Date().getHours()
      const mm = new Date().getMinutes() < 10 ? '0' + new Date().getMinutes() : new Date().getMinutes()
      const ss = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds() : new Date().getSeconds()
      return yy + '-' + MM + '-' + dd + ' ' + HH + ':' + mm + ':' + ss
    }

2. JS检测数组中重复出现的数据及其位置(很好用)

2.1 数组中是数字或者字符串

function findDuplicates(arr) {
  const duplicates = {};
  arr.forEach((item, index) => {
    if (arr.indexOf(item) !== index) {
      duplicates[item] = duplicates[item] || [];
      duplicates[item].push(index);
    }
  });
  return duplicates;
}
 
// 示例使用
const myArray = [1, 3, 2, 4, 2, 3, 5];
const duplicates = findDuplicates(myArray);
console.log(duplicates); // 输出: { 2: [2, 5], 3: [1, 4] }

2.2 数组中是json

// JS检测数组中重复出现的数据及其位置(json)
    findDuplicates(arr) {
      let duplicates = {};
      arr.forEach((item, index) => {
          if (!duplicates[item.name) {
            duplicates[item.name] = [];
          }
          duplicates[item.name].push(index);
      });
      return duplicates;
    }

arr=[{name:'zs',age:18},{name:'ls', age:18},{name:'ww',age:20},{name:'zs',age:18}]
console.log(this.findDuplicates(arr))

最后打印:

数组长度代表在数组中出现了几次,数组代表在数组中的位置

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要在前端获取当前时间年月日,你可以使用 JavaScript 的 Date 对象。 你可以使用如下代码来获取当前年份: ``` let date = new Date(); let year = date.getFullYear(); ``` 要获取月份,你可以使用 getMonth() 方法: ``` let month = date.getMonth() + 1; ``` 注意,getMonth() 方法返回的月份是从 0 开始的(0 表示一月,1 表示二月,依此类推),所以你需要加上 1。 要获取日,你可以使用 getDate() 方法: ``` let day = date.getDate(); ``` 例如,如果你想获取当前时间年月日,你可以使用如下代码: ``` let date = new Date(); let year = date.getFullYear(); let month = date.getMonth() + 1; let day = date.getDate(); console.log(`${year}-${month}-${day}`); // 输出当前年月日 ``` 希望这对你有帮助! ### 回答2: 要在前端获取当前时间的年、月、日,可以使用JavaScript的Date对象。 使用Date对象的getFullYear()方法可以获取当前的年份,而getMonth()方法会返回一个0-11之间的整数,表示当前的月份,因此我们需要加1来得到实际的月份。同理,getDate()方法可以获取当前的日期。 以下是一个示例代码: ```javascript // 创建一个Date对象 var currentDate = new Date(); // 获取年份 var year = currentDate.getFullYear(); // 获取月份(加1) var month = currentDate.getMonth() + 1; // 获取日期 var day = currentDate.getDate(); // 打印结果 console.log("当前时间:" + year + "年" + month + "月" + day + "日"); ``` 以上代码中,我们首先创建一个Date对象,然后分别使用getFullYear()、getMonth()和getDate()方法获取年、月、日,并将它们存储在变量中。最后,我们使用console.log()方法打印结果。 这样,就可以在前端获取到当前的时间年月日。 ### 回答3: 前端获取当前时间年月日有多种方式,下面我将介绍两种常用的方法。 方法一:使用JavaScript的Date对象 可以使用JavaScript的Date对象来获取当前时间年月日。具体步骤如下: 1. 创建一个Date对象:`var currentDate = new Date();` 2. 使用Date对象的方法获取当前时间年月日: - 年份:`var year = currentDate.getFullYear();` - 月份(注意返回的月份是从0开始的,所以需要加1):`var month = currentDate.getMonth() + 1;` - 日期:`var day = currentDate.getDate();` 示例代码如下: ``` var currentDate = new Date(); var year = currentDate.getFullYear(); var month = currentDate.getMonth() + 1; var day = currentDate.getDate(); ``` 方法二:使用Moment.js库 Moment.js是一个JavaScript的日期处理库,提供了简洁且易用的方式来处理日期和时间。使用Moment.js获取当前时间年月日的步骤如下: 1. 引入Moment.js库:在HTML文件中通过`<script>`标签引入Moment.js库。 2. 使用Moment.js获取当前时间年月日: - 年份:`var year = moment().format('YYYY');` - 月份:`var month = moment().format('MM');` - 日期:`var day = moment().format('DD');` 示例代码如下: ``` var year = moment().format('YYYY'); var month = moment().format('MM'); var day = moment().format('DD'); ``` 以上两种方法都可以在前端页面中获取当前时间年月日。根据具体需求选择合适的方法来实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值