首页 > 其他分享 >JS输出当前周一到周日范围时间

JS输出当前周一到周日范围时间

时间:2023-12-13 16:00:03浏览次数:21  
标签:输出 const currentTime index JS formatTime startTime Date 周日

网上搜的都感觉好复杂,看不懂,自己写了个,存着。

 1     //格式化时间
 2       const formatTime = function (date) {
 3         const year = date.getFullYear().toString();
 4         const month = (date.getMonth() + 1).toString().padStart(2, '0'); // 月份从0开始,因此需要加1
 5         const day = date.getDate().toString().padStart(2, '0');
 6         return `${year}年${month}月${day}日`
 7       }
 8       const currentTime = new Date();//当前时间
 9       10       //获取当前周几
11       const index = currentTime.getDay()
12       if (index === 0) {
13         //周日
14         const startTime = new Date(currentTime.getTime() - 6 * 24 * 3600 * 1000)
15         this.week = `${formatTime(startTime)} ~ ${formatTime(currentTime)}`
16       } else {
17         //周一及之后
18         const startTime = new Date(currentTime.getTime() - (index - 1) * 24 * 3600 * 1000)
19         const endTime = new Date(currentTime.getTime() + (7 - index) * 24 * 3600 * 1000)
20         this.week = `${formatTime(startTime)} ~ ${formatTime(endTime)}`
21       }

格式化函数可以自定义返回值,我这里只需要到日就行,所以就这么返回了。

this.week就是需要显示的字符串

标签:输出,const,currentTime,index,JS,formatTime,startTime,Date,周日
From: https://www.cnblogs.com/ricardox3/p/17899202.html

相关文章

  • 01C# 从Json文件中读取配置
    目的:从Json文件中读取配置 1)创建一个json文件,设置“如果较新则复制”{"Smtp":{"Server":"yx165.com","Name":"yx","Password":"123456"},"Person":{"Name":"......
  • 【Node.js】大前端技能最通俗易懂的讲解 快速入门必看
    目录1、概述前端工具VSCode安装2、NodeJS的安装3、NodeJS了解和快速入门4、NodeJS实现HttpServer服务5、NodeJS实现操作MySQL数据库Node.js是一个基于ChromeV8引擎的JavaScript运行环境,它允许开发者在服务器端执行Node.js是一个基于ChromeV8引擎的JavaScript运行环境,它允许开发者......
  • js实时显示当前时间(转载)
    <!DOCTYPEhtml><htmlxmlns="http://www.w3.org/1999/xhtml"><head><metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/><title>显示时间</title></head><body&g......
  • cesium.js入门基础教程二 (影像和标注)
    影像简介Cesium加载大的虚拟地球默认是有影像的,如图:默认使用的影像是“BingMapsAerial”:影像提供者ImageryProvider除了途中列出的一些影像,开发者可以通过Cesium中的ImagerProvider影像提供者类创建所需的影像,Cesium中提供了很多种ImageryProvider影像提供类(不包括抽象......
  • js中Array.from方法
    这个方法,主要用于将一个类似数组的东西变成为数组一.将一个字符串转化伪数组letstr='helloWord'letarr=Array.from(str)console.log(arr)//输出['h','e','l','l','o','W','o','r','......
  • js中数组map和集合map
    js中数组的map:使用情况:想要对一个数组进行操作,然后又不想改变原来的数组数据,还想基于原来数组的数据进行改造,那么可以使用map写法一:letarr=[1,2,3,4]letnewArr=arr.map(item=>{return++item})console.log(newArr,arr)//输出[2,3,4,5][1,2,3,4]letarr=[1......
  • 通过PowerShellPlus示例脚本学习PowerShell之-输出SQLServer服务属性
    ##=====================================================================##Title:Get-MSSQL-ServerAttrib-Csv##Description:ConnecttoSQLServerandoutputserverattributestoCSV##Author:Idera##Date:1/28/2009##Input......
  • js实现上传文件夹功能
    最近在研究上传文件夹功能,并上传到阿里云oss,研究了几天终于实现了。前端代码:添加“webkitdirectory”标签表示支持文件夹上传<inputtype='file'id="inputUploadDir"name="file"webkitdirectory>js代码部分$("#inputUploadDir").change(function(e){letfiles=t......
  • js上传文件夹的功能如何实现
    在JavaScript中无法直接上传整个文件夹,但可以通过以下步骤实现上传文件夹的功能:1.使用``标签来选择文件夹。该标签支持同时选择多个文件和文件夹。2.监听文件夹选择变化的事件,并获取所选择的文件和文件夹。constfileInput=document.querySelector('input[type="file"]');fil......
  • jsHTTP/HTML/浏览器
    1|前端基础1.1|HTTP/HTML/浏览器说一下http和https参考回答:https的SSL加密是在传输层实现的。(1)http和https的基本概念http:超文本传输协议,是互联网上应用最为广泛的一种网络协议,是一个客户端和服务器端请求和应答的标准(TCP),用于从WWW服务器传输超文本到本地浏......