jquery的各种时间的获取

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<div class="today">获取今天时间</div>
<div class="accuracy_time">获取今天时间准确时间</div>
<div class="weektoday">周几</div>
<div class="yesterday">昨天</div>
<div class="tomorrow">明天</div>
<div class="week_begin_end">本周开始到结束日期</div>
<div class="month_begin_end">本月开始到结束 有多少天</div>

<body>

</body>
<script src="./jquery-1.12.3.min.js"></script>
<script>
  $(".today").click(function () { //获取今天时间
    let date = new Date(); //当前的时间戳
    const year = date.getFullYear()
    let month = date.getMonth() + 1
    let day = date.getDate()
    if (month < 10) {
      month = '0' + month
    }
    if (day < 10) {
      day = '0' + day
    }
    console.log(year + '-' + month + '-' + day, '今天日期');
    console.log('----------');
  })


  $(".accuracy_time").click(function () { //获取今天时间准确时间
    let date = new Date(); //当前的时间戳
    const year = date.getFullYear()
    let month = date.getMonth() + 1
    let day = date.getDate()
    let hour = date.getHours()
    let minute = date.getMinutes()
    let second = date.getSeconds()
    if (month < 10) {
      month = '0' + month
    }
    if (day < 10) {
      day = '0' + day
    }
    hour = hour.toString().padStart(2, '0')
    minute = minute.toString().padStart(2, '0')
    second = second.toString().padStart(2, '0')
    console.log(year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second, '当前具体时间');
    console.log('----------');
  })


  $(".weektoday").click(function () { //周几
    let date = new Date(); //当前的时间戳
    const thisDay = new Date().getDay()
    const weekDay = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
    console.log(weekDay[thisDay] + '          周几');
    console.log('----------');
  })


  $(".yesterday").click(function () { //昨天
    let date = new Date(); //当前的时间戳
    date.setDate(date.getDate() - 1); //设置天数 -1 天,昨天的日期
    let year = date.getFullYear();//年
    let month = date.getMonth() + 1;//月
    let day = date.getDate();//日
    if (month < 10) {
      month = '0' + month
    }
    if (day < 10) {
      day = '0' + day
    }
    console.log(year + '-' + month + '-' + day, '昨天');
    console.log('----------');
  })

  $(".tomorrow").click(function () { //昨天
    let date = new Date(); //当前的时间戳
    date.setDate(date.getDate() + 1); //设置天数 +1 天,明天的日期
    let year = date.getFullYear();//年
    let month = date.getMonth() + 1;//月
    let day = date.getDate();//日
    if (month < 10) {
      month = '0' + month
    }
    if (day < 10) {
      day = '0' + day
    }
    console.log(year + '-' + month + '-' + day, '明天');
    console.log('----------');
  })


  $(".week_begin_end").click(function () { //本周开始到结束日期
    let weekList = [];
    let date = new Date();//当前的时间戳
    if (date.getDay() == "0") {
      date.setDate(date.getDate() - 6);
    } else {
      date.setDate(date.getDate() - date.getDay() + 1);
    }
    let myDate = date.getDate();
    let myMonth = date.getMonth() + 1;
    if (date.getDate() < 10) {
      myDate = '0' + myDate;
    }
    if (date.getMonth() + 1 < 10) {
      myMonth = '0' + myMonth;
    }
    weekList.push(date.getFullYear() + "-" + myMonth + "-" + myDate);
    for (var i = 0; i < 6; i++) {
      date.setDate(date.getDate() + 1);
      myDate = date.getDate();
      myMonth = date.getMonth() + 1;
      if (date.getDate() < 10) {
        myDate = '0' + myDate;
      }
      if (date.getMonth() + 1 < 10) {
        myMonth = '0' + myMonth;
      }
      weekList.push(date.getFullYear() + "-" + myMonth + "-" + myDate);
    }
    console.log(weekList, '本周开始到结束日期');
    console.log('----------');
  })


  $(".month_begin_end").click(function () {  //月初  月末  有多少天
    var nowDate = new Date();
    var cloneNowDate = new Date();
    var fullYear = nowDate.getFullYear();
    var month = nowDate.getMonth() + 1; // getMonth 方法返回 0-11,代表1-12月
    var endOfMonth = new Date(fullYear, month, 0).getDate(); // 获取本月最后一天
    function getFullDate(targetDate) {
      var D, y, m, d;
      if (targetDate) {
        D = new Date(targetDate);
        y = D.getFullYear();
        m = D.getMonth() + 1;
        d = D.getDate();
      } else {
        y = fullYear;
        m = month;
        d = date;
      }
      m = m > 9 ? m : '0' + m;
      d = d > 9 ? d : '0' + d;
      return y + '-' + m + '-' + d;
    };
    let endDate = getFullDate(cloneNowDate.setDate(endOfMonth));//当月最后一天
    let starDate = getFullDate(cloneNowDate.setDate(1));//当月第一天
    console.log(starDate, endDate, iDays(starDate, endDate), '月初  月末  有多少天'); //月初  月末  当前月共有多少天
    console.log('----------');
  })
  function iDays(sDate1, sDate2) {
    var aDate, oDate1, oDate2, iDays;
    aDate = sDate1.split("-");
    oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]);
    aDate = sDate2.split("-");
    oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]);
    iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24);
    return iDays + 1; //返回相差天数
  }


</script>

</html>

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值