zoukankan      html  css  js  c++  java
  • JS 获取昨天、今天、上一周、本周

    关注公众号: 微信搜索 web全栈进阶 ; 收货更多的干货

    开发日常,没什么技巧,主要是换算,直接看代码;复制粘贴即可用

    对应的是element-ui DatePicker 带快捷选项 时间控件,所以返回的是数组;

    export const getTime = function () {
      // 起止日期数组
      let dateTime = {
        // 昨天
        yesterday: [],
        // 今天
        today: [],
        // 上一周
        lastWeekDate: [],
        // 本周
        thisWeekDate: []
      }
      // 获取当前时间
      let currentDate = new Date()
      // 返回date是一周中的某一天
      let week = currentDate.getDay()
      // 返回date是一个月中的某一天
      // let month = currentDate.getDate()
      // 一天的毫秒数
      let millisecond = 1000 * 60 * 60 * 24
      // 减去的天数
      let minusDay = week !== 0 ? week - 1 : 6
    
      // 获得当前周的第一天
      let currentWeekDayOne = new Date(currentDate.getTime() - (millisecond * minusDay))
      // 上周最后一天即本周开始的前一天
      let priorWeekLastDay = new Date(currentWeekDayOne.getTime() - millisecond)
      // 上周的第一天
      let priorWeekFirstDay = new Date(priorWeekLastDay.getTime() - (millisecond * 6))
      // 上周第一天时间封装
      const start = `${priorWeekFirstDay.getFullYear()}-${supplement(priorWeekFirstDay.getMonth() + 1, 2)}-${supplement(priorWeekFirstDay.getDate(), 2)} 00:00:00`
      // 上周最后一天时间封装
      const end = `${priorWeekLastDay.getFullYear()}-${supplement(priorWeekLastDay.getMonth() + 1, 2)}-${supplement(priorWeekLastDay.getDate(), 2)} 23:59:59`
    
      // 本周 周一
      let monday = new Date(currentDate.getTime() - (minusDay * millisecond))
      // 本周 周日
      let sunday = new Date(monday.getTime() + (6 * millisecond))
      // 本周第一天时间封装
      const start1 = `${monday.getFullYear()}-${supplement(monday.getMonth() + 1, 2)}-${supplement(monday.getDate(), 2)} 00:00:00`
      // 本周最后一天时间封装
      const end2 = `${sunday.getFullYear()}-${supplement(sunday.getMonth() + 1, 2)}-${supplement(sunday.getDate(), 2)} 23:59:59`
      // 添加至上周数组
      dateTime.lastWeekDate.push(start, end)
      // 添加至本周数组
      dateTime.thisWeekDate.push(start1, end2)
    
      // 昨天
      dateTime.yesterday.push(`${currentDate.getFullYear()}-${supplement(currentDate.getMonth() + 1, 2)}-${supplement(currentDate.getDate() - 1, 2)} 00:00:00`, `${currentDate.getFullYear()}-${supplement(currentDate.getMonth() + 1, 2)}-${supplement(currentDate.getDate() - 1, 2)} 23:59:59`)
    
      // 今天
      dateTime.today.push(`${currentDate.getFullYear()}-${supplement(currentDate.getMonth() + 1, 2)}-${supplement(currentDate.getDate(), 2)} 00:00:00`, `${currentDate.getFullYear()}-${supplement(currentDate.getMonth() + 1, 2)}-${supplement(currentDate.getDate(), 2)} 23:59:59`)
      return dateTime
    }
    
  • 相关阅读:
    [原创]全新IFPGA-Cable----支持Xilinx/Altera/Lattice JTAG和UART
    [原创]Modelsim后仿真
    [原创]iFPGA-USB2.0 FT2232H USB & UART开发板使用说明
    [原创]X-HDL 4.2安装与使用
    [原创][Synth 8-2543] port connections cannot be mixed ordered and named ["*_Top.v":1151]
    [原创]..OBJgpio.axf: error: L6002U: Could not open file ..objgpio.o: No such file
    [原创]Zynq SDIO WIFI SotfAP调试
    [原创]时序图新画法
    [原创]基于Zynq SDIO WIFI 2.4G/5G SotfAP STA
    [原创]Zynq AXI-CDMA的使用
  • 原文地址:https://www.cnblogs.com/ljx20180807/p/14250131.html
Copyright © 2011-2022 走看看