zoukankan      html  css  js  c++  java
  • javascript 获取本周第一天和最后一天

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <title>查询</title>
    <style>
    label{
    line-height: 25px;
    }
    </style>

    </head>

    <body>

    </body>
    </html>
    <script>
    window.onload=function (){
    alert(getCurrentWeekFirstDay(new Date()))
    alert(getCurrentWeekLastDay(new Date()))
    }
    function getCurrentWeekFirstDay(date) {
    let weekFirstDay = new Date(date - (date.getDay() - 1) * 86400000)
    let firstMonth = Number(weekFirstDay.getMonth()) + 1

    if (firstMonth < 10) {
    firstMonth = '0' + firstMonth
    }
    let weekFirstDays = weekFirstDay.getDate();
    if (weekFirstDays < 10) {
    weekFirstDays = '0' + weekFirstDays;
    }
    return weekFirstDay.getFullYear() + '-' + firstMonth + '-' + weekFirstDays;
    }
    /**
    * 获取本周的最后一天
    * 返回格式: YYYY-mm-dd
    * 例子: 当日为: 2020-11-27
    * 返回日期为: 2020-11-29
    * */
    function getCurrentWeekLastDay(date) {
    let weekFirstDay = new Date(date - (date.getDay() - 1) * 86400000)
    let weekLastDay = new Date((weekFirstDay / 1000 + 6 * 86400) * 1000)
    let lastMonth = Number(weekLastDay.getMonth()) + 1
    if (lastMonth < 10) {
    lastMonth = '0' + lastMonth
    }
    let weekLastDays = weekLastDay.getDate();
    if (weekLastDays < 10) {
    weekLastDays = '0' + weekLastDays;
    }
    return weekFirstDay.getFullYear() + '-' + lastMonth + '-' + weekLastDays;
    }
    </script>

  • 相关阅读:
    IDEA如何打包可运行jar的一个问题。
    从一个脚本谈loadrunner的脚本初始化
    explain 用法详解
    linux zip 命令详解
    Jmeter使用——参数化
    Jmeter Constant Throughput Timer 使用
    产生随机时间的例子
    Mysql的列索引和多列索引(联合索引)
    Loadrunner负载机agent
    Spring context:property-placeholder 一些坑
  • 原文地址:https://www.cnblogs.com/DylanZ/p/15597776.html
Copyright © 2011-2022 走看看