前端練手小項目--自定義時間(html+css+js)

自定義時間

寫文章的因

關于要寫這篇文章的原因

  • 是記錄在工作上遇到的困難需求,
  • 是希望能給大家提供一些解決問題的思路

接下來我描述這個需求的多樣性,難點在哪。

  • 勾選勾選框開始時間與結束時間默認顯示昨天與今天。
  • 取消勾選框開始時間與結束時間清空。
  • 選擇開始時間,勾選框勾選,結束時間為今天。
    在用戶點擊 開始時間大于或者等于結束時間時,
    提示錯誤信息,開始時間清空,
    選擇結束時間時,小于或者等于開始時間,
    顯示報錯,結束時間清空。
  • 選擇結束時間,勾選框勾選,開始時間
    為所選結束時間的昨天。
    在用戶點擊結束時間小于或者等于結束時間時,
    提示錯誤信息,結束時間清空,
    選擇開始時間時,大于或者等于開始時間,
    顯示報錯,開始時間清空。
  • 其次在每次選擇時間的時候都要計算出開始時間與結束時間。
  • 用戶不能選擇限制范圍之外的時間

請添加圖片描述

1.效果演示

自定義時間效果展示

2.思路解析

2.1編寫html

該部分比較簡單,就一個錯誤提示div
一段文字,一個勾選框,兩個時間選擇框

<div id="errorMsg" style="color:red;margin-top:10px;margin-bottom:20px"></div><div><label for="select_time">自定義文件修改時間:</label><input type="checkbox" name="select_time" id='selectTime' onchange="changeStatus(event)" id="selectTime"><input type="date"  id='startTime' ref="startTime" onchange="getstartTime(event)" max=""> ---<input type="date" ref="endTime" id="endTime" onchange="getEndTime(event)" max=""></div>

2.2編寫自動得到當前時間的函數

這里的難點是在日期框顯示‘yyyy-mm-dd’,
就需要對獲取到的值進行處理,

 function GetDateStr(AddDayCount) { var dd = new Date(); dd.setDate(dd.getDate()+AddDayCount);//獲取AddDayCount天后的日期 var y = dd.getFullYear(); var m = (dd.getMonth()+1);//獲取當前月份的日期 var d = dd.getDate(); if(m>=10&&m>=10){return y+"-"+m+"-"+d; }else if(m<10&&d>=10){return y+"-"+'0'+m+"-"+d; }else if(m<10&&d<10){return y+"-"+'0'+m+"-"+'0'+d; } }

2.3限制用戶選擇日期

該功能通過給input添加一個max屬性即可,
但是該max屬性值必須是max='yyyy-mm-dd’的形式。

window.onload=function(){document.getElementById('startTime').setAttribute('max',this.GetDateStr(-1))document.getElementById('endTime').setAttribute('max',this.GetDateStr(0))    
}

2.4關于時間的計算

時間的計算問題,因為我們通過Date.parse()
格式化時間得到的時間是東八區的時間,要想得到
零點的時間需要減去8個小時。
開始時間等于start=Date.parse(‘yyyy-mm-dd’)/1000-83600
結束時間等于end=Date.parse(‘yyyy-mm-dd’)/1000+16
3600
因為我們需要的時間是昨天的零點,以及今天的24點

  //得到時間為svar  modification_time_start=null,modification_time_end=null,modification_time_start=Date.parse(this.GetDateStr(-1)) / 1000-8*3600modification_time_end=Date.parse(this.GetDateStr(0)) / 1000+16*3600  

2.5用戶選擇開始時間

當用戶選擇開始時間,結束時間調用 GetDateStr()并賦值。
勾選框勾選,給該元素設置屬性checked為true。
然后就是對開始時間與結束時間的判斷來決定
是否顯示錯誤提示,以及清空開始框。

function getstartTime() {$('#selectTime').attr('checked',true)//給勾選框添加屬性,讓其處于勾選狀態$('#selectTime').prop('checked',true)modification_time_start = Date.parse($('#startTime').val())/1000-8*3600// 用戶只選中開始時間,結束時間默認為當前時間。并重新賦值if(count||modification_time_end===null){document.getElementById('endTime').value=this.GetDateStr(0)count=!count}if(document.getElementById('startTime').value ==='' && document.getElementById('endTime').value===''){$('#selectTime').attr('checked',false)$('#selectTime').prop('checked',false)}// document.getElementById('endTime').value=this.GetDateStr(0)document.getElementById('startTime').value=$('#startTime').val()modification_time_end= Date.parse($('#endTime').val())/1000+16*3600if(modification_time_end<=modification_time_start || $('#endTime').val()<=$('#startTime').val()){//alert('所選時間大于結束時間,請重新選擇時間')document.getElementById('errorMsg').innerText='所選時間大于或等于結束時間,請重新選擇時間'document.getElementById('startTime').value=''document.getElementById('endTime').value=$('#endTime').val()}else{document.getElementById('errorMsg').innerText=''}console.log('開始時間===》',modification_time_start,'結束時間===》', modification_time_end)}

2.6用戶選擇開始時間

當用戶選擇結束時間,結束時間調用 GetDateStr()并賦值。
勾選框勾選,給該元素設置屬性checked為true。
然后就是對開始時間與結束時間的判斷來決定
是否顯示錯誤提示,以及清空結束時間框。

 function getEndTime() {$('#selectTime').attr('checked',true)$('#selectTime').prop('checked',true)      modification_time_end = Date.parse($('#endTime').val())/1000+16*3600document.getElementById('endTime').value=$('#endTime').val()modification_time_start= Date.parse($('#startTime').val())/1000-8*3600if(document.getElementById('startTime').value ==='' && document.getElementById('endTime').value===''){$('#selectTime').attr('checked',false)$('#selectTime').prop('checked',false)}//當用戶只選中最后時間時,開始時間應該有個默認值。該最后時間的前一天,重新給開始時間賦值 if(modification_time_end<=modification_time_start || $('#endTime').val()<=$('#startTime').val()){//alert('所選時間小于開始時間,請重新選擇時間')document.getElementById('errorMsg').innerText='所選時間小于或等于開始時間,請重新選擇時間'document.getElementById('endTime').value=''document.getElementById('startTime').value=$('#startTime').val()}else{document.getElementById('errorMsg').innerText=''}if(count){let date=new Date(Date.parse($('#endTime').val())-24*3600*1000)Y = date.getFullYear() + '-'M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-'D = (date.getDate()< 10 ? '0'+date.getDate() : date.getDate())document.getElementById('startTime').value=Y+M+Dmodification_time_start=Date.parse(Y+M+D)/1000-8*3600count=!count}console.log('開始時間===》',modification_time_start,'結束時間===》', modification_time_end)}

2.7總結

在該需求中,我們學到了那些內容

  • 計算時間的準確性(開始時間的0點,結束時間的24點)以及關于使用Date.parse(‘yyyy-mm-dd’)的值為東八區的值。
    +怎么得到當前時間,以及怎么將該值賦值到日期框中document.getElementById(‘startTime’).value=‘yyyy-mm-dd’,
  • 通過jquery改變勾選框的勾選狀態$(’#selectTime’).attr(‘checked’,true) $(’#selectTime’).prop(‘checked’,true)

3.完整代碼

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.js"></script>
</head>
<body><div id="errorMsg" style="color:red;margin-top:10px;margin-bottom:20px"></div><div><label for="select_time">自定義文件修改時間:</label><input type="checkbox" name="select_time" id='selectTime' onchange="changeStatus(event)" id="selectTime"><input type="date"  id='startTime' ref="startTime" onchange="getstartTime(event)" max=""> ---<input type="date" ref="endTime" id="endTime" onchange="getEndTime(event)" max=""></div><script>var  modification_time_start=null,modification_time_end=null,count=truefunction GetDateStr(AddDayCount) { var dd = new Date(); dd.setDate(dd.getDate()+AddDayCount);//獲取AddDayCount天后的日期 var y = dd.getFullYear(); var m = (dd.getMonth()+1);//獲取當前月份的日期 var d = dd.getDate(); if(m>=10&&m>=10){return y+"-"+m+"-"+d; }else if(m<10&&d>=10){return y+"-"+'0'+m+"-"+d; }else if(m<10&&d<10){return y+"-"+'0'+m+"-"+'0'+d; } }window.onload=function(){document.getElementById('startTime').setAttribute('max',this.GetDateStr(-1))document.getElementById('endTime').setAttribute('max',this.GetDateStr(0))    }function changeStatus(event) {if (event.target.checked) {modification_time_start=Date.parse(this.GetDateStr(-1)) / 1000-8*3600modification_time_end=Date.parse(this.GetDateStr(0)) / 1000+16*3600document.getElementById('startTime').value = this.GetDateStr(-1)document.getElementById('endTime').value = this.GetDateStr(0)console.log('開始時間===》',modification_time_start,'結束時間===》', modification_time_end)}else{document.getElementById('startTime').value =nulldocument.getElementById('endTime').value=nullmodification_time_end=nullmodification_time_start=null}}function getstartTime() {$('#selectTime').attr('checked',true)$('#selectTime').prop('checked',true)modification_time_start = Date.parse($('#startTime').val())/1000-8*3600// 用戶只選中開始時間,結束時間默認為當前時間。并重新賦值if(count||modification_time_end===null){document.getElementById('endTime').value=this.GetDateStr(0)count=!count}if(document.getElementById('startTime').value ==='' && document.getElementById('endTime').value===''){$('#selectTime').attr('checked',false)$('#selectTime').prop('checked',false)}// document.getElementById('endTime').value=this.GetDateStr(0)document.getElementById('startTime').value=$('#startTime').val()modification_time_end= Date.parse($('#endTime').val())/1000+16*3600if(modification_time_end<=modification_time_start || $('#endTime').val()<=$('#startTime').val()){//alert('所選時間大于結束時間,請重新選擇時間')document.getElementById('errorMsg').innerText='所選時間大于或等于結束時間,請重新選擇時間'document.getElementById('startTime').value=''document.getElementById('endTime').value=$('#endTime').val()}else{document.getElementById('errorMsg').innerText=''}console.log('開始時間===》',modification_time_start,'結束時間===》', modification_time_end)}function getEndTime() {$('#selectTime').attr('checked',true)$('#selectTime').prop('checked',true)      modification_time_end = Date.parse($('#endTime').val())/1000+16*3600document.getElementById('endTime').value=$('#endTime').val()modification_time_start= Date.parse($('#startTime').val())/1000-8*3600if(document.getElementById('startTime').value ==='' && document.getElementById('endTime').value===''){$('#selectTime').attr('checked',false)$('#selectTime').prop('checked',false)}//當用戶只選中最后時間時,開始時間應該有個默認值。該最后時間的前一天,重新給開始時間賦值 if(modification_time_end<=modification_time_start || $('#endTime').val()<=$('#startTime').val()){//alert('所選時間小于開始時間,請重新選擇時間')document.getElementById('errorMsg').innerText='所選時間小于或等于開始時間,請重新選擇時間'document.getElementById('endTime').value=''document.getElementById('startTime').value=$('#startTime').val()}else{document.getElementById('errorMsg').innerText=''}if(count){let date=new Date(Date.parse($('#endTime').val())-24*3600*1000)Y = date.getFullYear() + '-'M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-'D = (date.getDate()< 10 ? '0'+date.getDate() : date.getDate())document.getElementById('startTime').value=Y+M+Dmodification_time_start=Date.parse(Y+M+D)/1000-8*3600count=!count}console.log('開始時間===》',modification_time_start,'結束時間===》', modification_time_end)}</script>
</body>
</html>

好了這次的文章就到這了
如果覺得還不錯的話,幫忙點個關注吧
希望能給博主點贊哎🎨,評論🧶,收藏🥼三連一波加粗樣式

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/40312.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/40312.shtml
英文地址,請注明出處:http://en.pswp.cn/news/40312.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

如何查看線程在哪個cpu核上

1、ps -eLF查看PSR值 2、 taskset -pc $pid&#xff08;進程/線程&#xff09; 參考鏈接&#xff1a;https://blog.csdn.net/test1280/article/details/87993669

Ubuntu修改設置系列--修改ssh端口號的方法(有示例)

原文網址&#xff1a;Ubuntu修改設置系列--修改ssh端口號的方法(有示例)_IT利刃出鞘的博客-CSDN博客 簡介 說明 本文介紹Ubuntu修改ssh端口號的方法(有示例)。 要達成的目標 ssh添加一個端口&#xff1a;3333&#xff0c;關閉原來的22端口。 1.修改端口 修改配置文件/et…

thingsboard編譯安裝踩坑記錄

thingsboard編譯安裝踩坑記錄 一、編譯&#xff1a;二、運行 朋友的thingsboard沒人維護&#xff0c;要裝新的服務器&#xff0c;啥文檔也沒有&#xff0c;就讓參考官網的文檔&#xff0c;版本也比較老3.2.2的&#xff0c;拿過來試了試記錄下踩坑的地方。 一、編譯&#xff1a;…

get與post如何拼接url與數據的靈活處理,循環的重要性。

get與post拼接url地址不同&#xff1a; let postData {method: "post",data: {op: "/api/setting/maintenanceperiod?period"this.authorizationCode,loadingConfig: {},data: {period:this.authorizationCode}}}; if(this.editData.id){let postData …

Nginx運行Vue項目:基本運行

需求 在Nginx服務器中&#xff0c;運行Vue項目。 說明 Vue項目打包生成的生產文件&#xff0c;是無法直接在瀏覽器打開的。需要放到Nginx服務器中&#xff0c;才能夠訪問。 本文章只介紹最基本的情況&#xff1a;Nginx中運行一個Vue項目。 實際生產環境&#xff0c;一個Ng…

mysql 批量給數據表和字段添加注釋

1、用命令行導出 mysql數據庫中的所有表 首先查看 mysql 的配置文件 “/etc/my.cnf ”&#xff0c;配置中找到 datadir 目錄&#xff0c; 將文件導出到 datadir 目錄下 我的 datadir 目錄是&#xff1a; /var/lib/mysql 連接mysql&#xff0c;執行導出命令 SELECT TABLE_NAM…

解密 AI 客服;在不同硬件設備上運行大型語言模型的可能性

&#x1f989; AI新聞 &#x1f680; 微軟必應首席執行官稱必應聊天優于OpenAI的GPT-4&#xff0c;但成本更高 摘要&#xff1a;微軟必應的首席執行官米哈伊爾?帕拉欣表示&#xff0c;必應聊天表現優于OpenAI的GPT-4&#xff0c;但使用了更高成本的檢索增強推理技術。必應聊…

中科億海微ROM使用

標題 ROM&#xff08;Read-Only Memory&#xff0c;只讀存儲器&#xff09;是一種在FPGA&#xff08;Field-Programmable Gate Array&#xff0c;現場可編程門陣列&#xff09;中常用的存儲器類型。與RAM&#xff08;Random Access Memory&#xff0c;機存取存儲器&#xff09;…

Nginx安全加固,版本隱藏及HTTP請求頭修改方法

1 隱藏nginx版本號 1.1 引言 nginx作為目前較為流行的http server軟件&#xff0c;其相關的安全漏洞也非常多&#xff0c;攻擊者可以根據我們的nginx版本來了解到相關的漏洞從而針對性的進行攻擊。 通過新版本的nginx都會修復一些老版本的已知漏洞&#xff0c;但有時候我們生…

二刷LeetCode--148. 排序鏈表(C++版本),必會題,思維題

思路&#xff0c;本題其實考察了兩個點&#xff1a;合并鏈表、鏈表切分。首先從1開始&#xff0c;將鏈表切成一段一段&#xff0c;因為需要使用歸并&#xff0c;所以下一次的切分長度應該是當前切分長度的二倍&#xff0c;每次切分&#xff0c;我們拿出兩段&#xff0c;然后將第…

虛擬機與Java虛擬機介紹

1、虛擬機 所謂虛擬機&#xff08;Virtual Machine&#xff09;&#xff0c;就是一臺虛擬的計算機。它是一款軟件&#xff0c;用來執行一系列虛擬計算機指令。大體上&#xff0c;虛擬機可以分為系統虛擬機和程序虛擬機。大名鼎鼎的Visual Box&#xff0c;VMware就屬于 系統虛…

提示丟失vcomp140.dll怎么辦?如何快速修復vcomp140.dll丟失問題

最近我遇到了一個程序啟動失敗的問題&#xff0c;錯誤提示顯示缺少了vcomp140.dll文件。經過一番研究和嘗試&#xff0c;我終于成功修復了這個問題。在這里&#xff0c;我將分享一下我的修復方法。 目錄 vcomp140.dll是什么&#xff1f; 如何快速修復呢&#xff1f; vcomp140…

sCrypt編程馬拉松于8月13日在復旦大學成功舉辦

繼6月在英國Exeter大學成功舉辦了為期一周的區塊鏈編程馬拉松后&#xff0c;美國sCrypt公司創始人兼CEO劉曉暉博士帶領核心團隊成員王一強、鄭宏鋒、周全&#xff0c;于8月13日在復旦大學再次成功舉辦了一場全新的sCrypt編程馬拉松。 本次活動由上海可一澈科技有限公司與復旦大…

C++筆記之花括號和圓括號初始化區別,列表初始化和初始化列表區別

C筆記之花括號和圓括號初始化區別&#xff0c;列表初始化和初始化列表區別 code review! 文章目錄 C筆記之花括號和圓括號初始化區別&#xff0c;列表初始化和初始化列表區別1.花括號{}進行初始化和圓括號()進行初始化2.列表初始化&#xff08;list initialization&#xff0…

Vitis高層次綜合學習——FPGA

高層次綜合 什么是高層次綜合&#xff1f;就是使用高級語言&#xff08;如C/C&#xff09;來編寫FPGA算法程序。 在高層次綜合上并不需要制定微架構決策&#xff0c;如創建狀態機、數據路徑、寄存器流水線等。這些細節可以留給 HLS 工具&#xff0c;通過提供輸入約束&#xff…

專訪阿里云席明賢,視頻云如何運用大模型與小模型來破繭升級2.0

不久前&#xff0c;LiveVideoStack與阿里云視頻云負責人席明賢&#xff08;花名右賢&#xff09;展開一場深度的對話&#xff0c;一個是圈內專業的社區媒體&#xff0c;一個是20年的IT老兵&#xff0c;雙方有交集、有碰撞、有火花。 面對風云變幻的內外環境&#xff0c;阿里云…

未來數字銀行的樣子

對銀行長期發展來講&#xff0c;這意味著將關閉和減少 低效率的實體分行&#xff0c;加速向數字化發展。實現成本節省和 IT 預算提效的需求&#xff0c;將為數字柜臺和銀行代理點創造新的機遇。 一個嶄新的世界&#xff1a;未來數字銀行趨勢圖 現在是銀行迎頭趕上并為客戶提供超…

拿來即用,自己封裝的 axios

文章目錄 一、需求二、分析1. 安裝axios2. 新建一個 ts 文件&#xff0c;封裝 axios3. store 存放 token 信息4. 使用5. 文件 type.js 一、需求 在日常開發中&#xff0c;我們會經常用到 axios &#xff0c;那么如何在自己的項目中自己封裝 axios 二、分析 1. 安裝axios np…

jenkins使用

安裝插件 maven publish over ssh publish over ssh 會將打包后的jar包&#xff0c;通過ssh推送到指定的服務器上&#xff0c;&#xff0c;在jenkins中設置&#xff0c;推送后腳本&#xff0c;實現自動部署jar包&#xff0c;&#xff0c; 裝了這個插件之后&#xff0c;可以在項…

非計算機科班背景者順利轉碼計算機領域:策略與前景展望

方向一&#xff1a;如何規劃才能實現轉碼&#xff1f; 對于非計算機科班背景的人想要順利轉碼進入計算機領域&#xff0c;規劃是至關重要的。以下是一些建議&#xff0c;可以幫助你在轉碼過程中更加順利&#xff1a; 自我評估和目標設定&#xff1a; 首先&#xff0c;你需要明…