JS获取日期,浏览器高度

2023-11-13

1、获取当前日期向前推x天或者向后推x天,并已年-月-日 时:分:秒的格式返回值

// 获取往前后各推x天
function getDateOfDelayOrAdvance(delay, advance) {
  // delay:往后推x天
  // advance:往前推x天
  const todaydate = new Date()
  const enddate = new Date(todaydate.valueOf() - advance * 24 * 60 * 60 * 1000)
  const beforedate = new Date(todaydate.valueOf() + delay * 24 * 60 * 60 * 1000)
  const firstdate = beforedate.getFullYear() + '-' + ((beforedate.getMonth() + 1) < 10 ? '0' + (beforedate.getMonth() + 1) : (beforedate.getMonth() + 1)) + '-' + (beforedate.getDate() < 10 ? '0' + beforedate.getDate() : beforedate.getDate())
  const lastdate = enddate.getFullYear() + '-' + ((enddate.getMonth() + 1) < 10 ? '0' + (enddate.getMonth() + 1) : (enddate.getMonth() + 1)) + '-' + (enddate.getDate() < 10 ? '0' + enddate.getDate() : enddate.getDate())
  return [lastdate, firstdate]
}

2、获取当前日期并格式化

// 获取并格式化当前日期
gettodaydate() {
  // 获取并格式化当前日期
  const 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
  const today = year + '-' + month + '-' + day
  return today
},

3、获取当前时间的秒级时间戳

localtime = (new Date()).valueOf()

4、获取浏览器的高度(在vue中的created或者mounted中调用此方法即可)

// 通过浏览器高度自动计算表格的高度
getClientHeight() {
  //在刚加载页面的时候,获取浏览器高度
  // 动态计算表格高度
  const windowHeight = document.documentElement.clientHeight || document.bodyclientHeight
  // 设置表格的高度(也可以是其他的高度),数值"210"根据需要调整
  this.TableHeight = windowHeight - 210
  // 通过onresize监听浏览器高度和宽度变化,动态给要设置高度的元素赋值
  window.onresize = () => {
	return (() => {
	  // 动态计算表格高度
	  const windowHeight = document.documentElement.clientHeight || document.bodyclientHeight
	  // 设置表格的高度(也可以是其他的高度),数值"210"根据需要调整
	  this.TableHeight = windowHeight - 210
	})()
  }
},

5、通过传入的日期,判断属于周几

// 通过传入的日期,判断属于周几
changeday(val) {
  const weekDay = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
  const index = new Date(Date.parse(val)).getDay()
  const redata = [weekDay[index], val.slice(8, 10)]
  return redata
},

6、获取当前日期所属周的第一天和最后一天

// 获取当前日期所属周的第一天和最后一天
getWeektime() {
  // 获取当前日期所属周的第一天和最后一天
  var date = new Date()
  // 本周一的日期
  date.setDate(date.getDate() - date.getDay() + 1)
  const begin = date.getFullYear() + '-' + ((date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1)) + '-' + (date.getDate() < 10 ? '0' + date.getDate() : date.getDate())
  // 本周日的日期
  date.setDate(date.getDate() + 6)
  const end = date.getFullYear() + '-' + ((date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1)) + '-' + (date.getDate() < 10 ? '0' + date.getDate() : date.getDate())
  return [begin, end]
},

7、计算获取本周和下周的日期

// 计算获取本周和下周的日期
getWeekDate() {
  // 获取当前时间
  const data = new Date()
  // 获取当前时间对应的上周六的日期
  const LastWeek6 = new Date(data.getTime() - (1 + data.getDay()) * 24 * 60 * 60 * 1000)
  // 将日期格式改为:年-月-日
  const LastWeekSix = LastWeek6.getFullYear() + '-' + ((LastWeek6.getMonth() + 1) < 10 ? '0' + (LastWeek6.getMonth() + 1) : (LastWeek6.getMonth() + 1)) + '-' + (LastWeek6.getDate() < 10 ? '0' + LastWeek6.getDate() : LastWeek6.getDate()) + ' 00:00:00'
  console.log('上周六日期===>', LastWeekSix)
  const ThisWeek5 = new Date(data.getTime() + (5 - data.getDay()) * 24 * 60 * 60 * 1000)
  // 将日期格式改为:年-月-日
  const ThisWeekFive = ThisWeek5.getFullYear() + '-' + ((ThisWeek5.getMonth() + 1) < 10 ? '0' + (ThisWeek5.getMonth() + 1) : (ThisWeek5.getMonth() + 1)) + '-' + (ThisWeek5.getDate() < 10 ? '0' + ThisWeek5.getDate() : ThisWeek5.getDate()) + ' 00:00:00'
  console.log('这周五日期===>', ThisWeekFive)
  const ThisWeek6 = new Date(data.getTime() + (6 - data.getDay()) * 24 * 60 * 60 * 1000)
  // 将日期格式改为:年-月-日
  const ThisWeekSix = ThisWeek6.getFullYear() + '-' + ((ThisWeek6.getMonth() + 1) < 10 ? '0' + (ThisWeek6.getMonth() + 1) : (ThisWeek6.getMonth() + 1)) + '-' + (ThisWeek6.getDate() < 10 ? '0' + ThisWeek6.getDate() : ThisWeek6.getDate()) + ' 00:00:00'
  console.log('这周六日期===>', ThisWeekSix)
  const nextWeek5 = new Date(data.getTime() + (12 - data.getDay()) * 24 * 60 * 60 * 1000)
  // 将日期格式改为:年-月-日
  const nextWeekFive = nextWeek5.getFullYear() + '-' + ((nextWeek5.getMonth() + 1) < 10 ? '0' + (nextWeek5.getMonth() + 1) : (nextWeek5.getMonth() + 1)) + '-' + (nextWeek5.getDate() < 10 ? '0' + nextWeek5.getDate() : nextWeek5.getDate()) + ' 00:00:00'
  console.log('下周五日期===>', nextWeekFive)

  // 返回日期参数
  return [LastWeekSix, ThisWeekFive, ThisWeekSix, nextWeekFive]
},

8、计算开始时间和结束时间之间的相差天数

// 计算开始时间和结束时间之间的相差天数
getDaysBetween(dateString1, dateString2) {
  // 计算开始时间和结束时间之间的相差天数
  const startDate = Date.parse(dateString1)
  const endDate = Date.parse(dateString2)
  const days = (endDate - startDate) / (1 * 24 * 60 * 60 * 1000)
  return days + 1
},

9、判断某一个时间是否在一个时间段范围内

idDuringDate(curDate, beganDate, endDate) {
  // 判断某一个时间是否在一个时间段范围内
  curDate = new Date(curDate)
  beganDate = new Date(beganDate)
  endDate = new Date(endDate)
  if (curDate >= beganDate && curDate <= endDate) {
	return 'true'
  } else {
	return 'flase'
  }
},

10、获取当前月份的第一天和最后一天

get_first_day() {
  // 获取当前月份的第一天
  var date = new Date()
  date.setDate(1)
  var month = parseInt(date.getMonth() + 1)
  var day = date.getDate()
  if (month < 10) month = '0' + month
  if (day < 10) day = '0' + day
  return (date.getFullYear() + '-' + month + '-' + day)
},
get_last_day() {
  // 获取当前月份的最后一天
  var date = new Date()
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  month = month < 10 ? '0' + month : month
  var day = new Date(year, month, 0)
  return (year + '-' + month + '-' + day.getDate())
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

JS获取日期,浏览器高度 的相关文章

随机推荐

  • 软考-嵌入式系统设计师-笔记:标准化知识

    文章目录 国际标准 国家标准 行业标准 企业标准基本知识 嵌入式系统相关标准 国际标准 国家标准 行业标准 企业标准基本知识 国际标准 有效期5年 ISO 标准号 杠 分标准号 冒号 发布年号 方括号中的内容可有可无 国家标准 GB 中国
  • Python爬取携程酒店信息

    代码 from selenium import webdriver from time import sleep import xlwt 进行excel操作 谷歌驱动 告诉电脑在哪打开浏览器 driver webdriver Chrome
  • cloudflare tunnel搭建网站

    文章目录 0 写在开头 1 一些准备工作 2 Cloudflare Tunnel 3 最后 0 写在开头 如果你刚接触web项目 想让你的项目部署在公网上 但只是兴趣使然 并不打算花费金钱长期运行 或许本博客对你会有所帮助 1 一些准备工作
  • Cookie和Session 登录

    Cookie 实现免登陆和Session 01 需求说明 完成用户登录功能 登录成功后跳到成功页面 显示用户名 登录失败可以跳回登录页面 登录成功后后续操作均能显示当前登录的用户名 02 完成代码 DologinServlet java O
  • 数据挖掘实验-week8-关联规则挖掘(Association Rule Mining)

    Contents 0 引言 0 1 关联规则挖掘 0 2 Apriori算法 实验 Step 1 Familiarize yourself with the arules package in R 1 1 Load the package
  • python如何输出数字后三位

    想要输出数字的后三位 首先需要了解一些基础知识 在Python中 浮点数可以用round 函数来保留小数位数 这个函数有两个参数 要操作的数字和要保留的小数位数 例如 round 3 1415926 3 将返回3 142 其中第二个参数3表
  • Electron+Vue3+TypeScript+Vite +Vue.Draggable 完成事务拖动

    原文地址 Electron Vue3 TypeScript Vite Vue Draggable 完成事务拖动 前言 有了简单的框架 下面来点实际业务相关的操作吧 集成 Vue Draggable 将每一项任务拖拽任务到每个分类吧 Vue
  • Java连接MySQL数据库并实现增删改查

    首先我们需要连接数据库 我这里选择的是MySQL数据库 我们需要做的第一件事就是加载驱动 采用下列语句加载MySQL驱动 Class forName com mysql jdbc Driver 驱动加载成功后就可以创建连接对象 这里会使用到
  • 斐波那契数列计算 Python编程

    问题描述 斐波那契数列如下 F 0 0 F 1 1 F n F n 1 F n 2 编写一个计算斐波那契数列的函数 采用递归方式 输出不超过n的所有斐波那契数列元素 调用上述函数 完成如下功能 用户输入一个整数n 输出所有不超过n的斐波那契
  • Markdown表格实现合并单元格

    Markdown表格实现合并单元格 markdown基本语法的表格没有办法实现合并单元格功能 但是在实际使用中很多时候需要合并单元格 例如需要写一个这样非连续的寄存器说明 markdown语法的表格 参数 额定值 最大结温 150 C DV
  • Node写博客--用户登录和用cookie保存用户登录状态

    1 首先在index js中加入用户登录的ajax数据传输 登录 loginBox find button on click function 通过ajax提交请求 ajax type post url api user login dat
  • FPGA零基础学习之Vivado-TLC5620驱动教程

    FPGA零基础学习之Vivado TLC5620驱动教程 本系列将带来FPGA的系统性学习 从最基本的数字电路基础开始 最详细操作步骤 最直白的言语描述 手把手的 傻瓜式 讲解 让电子 信息 通信类专业学生 初入职场小白及打算进阶提升的职业
  • 创建Metasploit Payloads

    Often one of the most useful and to the beginner underrated abilities of Metasploit is the msfpayload module Multiple pa
  • 虚拟机后台运行怎么彻底关闭?

    1 首先启动vmware workstation软件 2 打开之后 在虚拟机软件的左侧工具栏中打开 编辑 工具 3 然后在弹出来的下拉菜单中使用鼠标左键点击菜单中的 首选项 4 在 工作区 选项找到 显示托盘图标 把托盘图标默认的 始终 改
  • 索引以及存储过程和存储函数介绍

    目录 索引 创建索引 创建表时创建索引 在已经存在的表中创建索引 使用alter table 语句创建 使用create index语句创建 删除索引 存储过程与函数 创建存储过程和函数 创建存储过程 创建存储函数 调用存储过程和函数 调用
  • Weblogic自动部署与手动部署 异常记录与解决方法

    weblogic在控制台部署项目时提示 The application mycim2Ejb was autodeployed and may only be redeployed by touching REDEPLOY file in t
  • http长连接

    http长连接与短连接 http协议是基于TCP协议的 http长连接即指的是TCP的长连接 TCP协议是一个面向连接 可靠 有着拥塞控制以及流量控制的传输层协议 http长连接问题由来 http是一个无状态的协议 简单点来说 如果同一个浏
  • 生产者―消费者问题算法的实现

    实验目的 掌握进程 线程的概念 熟悉相关的控制语 掌握进程 线程间的同步原理和方法 掌握进程 线程间的互斥原理和方法 掌握使用信号量原语解决进程 线程间互斥和同步方法 实验思路 生产者的主要作用是生成一定量的数据放到缓冲区中 然后重复此过程
  • Android数据加密之Rsa/MD5/Aes/Des加密算法归纳总结

    加密算法 1 加密算法通常分为对称性加密算法和非对称性加密算法 对于对称性加密算法 信息接收双方都需事先知道密匙和加解密算法且其密匙是相同的 之后便是对数据进行 加解密了 非对称算法与之不同 发送双方A B事先均生成一堆密匙 然后A将自己的
  • JS获取日期,浏览器高度

    1 获取当前日期向前推x天或者向后推x天 并已年 月 日 时 分 秒的格式返回值 获取往前后各推x天 function getDateOfDelayOrAdvance delay advance delay 往后推x天 advance 往前