javascript顯示上周、上個月日期的處理方法
本文實(shí)例介紹了javascript一周前、一個月前的實(shí)現(xiàn)代碼,對于javascript日期處理進(jìn)行了簡單分析,分享給大家供大家參考,具體內(nèi)容如下
<html> <head> <title></title> <script src="../Script/jQuery/jquery-1.6.2.min.js" type="text/javascript"></script> <script src="../Script/MTHCRMWidget/MTHCRMWidget.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { myClick();//點(diǎn)擊事件觸發(fā) }) //專門包裝點(diǎn)擊事件; function myClick() { $(".tbBtn").click(function () { var sid = $(this).attr("id"); var agoDate = ""; var Cdate = new Date(); if (sid == "CbtnNull") { $("#txtCallCycleBegin").val(""); $("#txtCallCyclecurrend").val(""); } else if (sid == "CbtnMoon") { agoDate = ProcessDate(30); $("#txtCallCycleBegin").val("{0}-{1}-{2}".format(agoDate.Year, agoDate.Moon, agoDate.Day)); $("#txtCallCyclecurrend").val("{0}-{1}-{2}".format(Cdate.getFullYear(), Cdate.getMonth() + 1, Cdate.getDate())); } else { agoDate = ProcessDate(7); $("#txtCallCycleBegin").val("{0}-{1}-{2}".format(agoDate.Year, agoDate.Moon, agoDate.Day)); $("#txtCallCyclecurrend").val("{0}-{1}-{2}".format(Cdate.getFullYear(), Cdate.getMonth() + 1, Cdate.getDate())); } }) } //處理日期的函數(shù),返回一個字面量; function ProcessDate(type) { //1.0獲取現(xiàn)在時間的年月日: var currentTime = new Date("2016-01-02"); //得到當(dāng)前的時間 var currentYear = currentTime.getFullYear(); //得到當(dāng)前的年份 var currentMoon = currentTime.getMonth() + 1; //得到當(dāng)前的月份(系統(tǒng)默認(rèn)為0-11,所以要加1才算是當(dāng)前的月份) var currentDay = currentTime.getDate(); //得到當(dāng)前的天數(shù) //2.0獲取當(dāng)前時間的一個月內(nèi)的年月日:(一個月內(nèi)的大眾業(yè)務(wù)需求為:當(dāng)前時間的月份-1,當(dāng)前時間的天數(shù)+1) var agoDay = ""; var agoMoon = currentMoon; var agoYear = currentYear; var max = ""; switch (type) { case 30: agoDay = currentDay + 1; agoMoon = currentMoon - 1; max = new Date(agoYear, agoMoon, 0).getDate(); //獲取上個月的總天數(shù) break; case 7: agoDay = currentDay - 6; if (agoDay < 0) { agoMoon = currentMoon - 1;//月份減1 max = new Date(agoYear, agoMoon, 0).getDate(); //獲取上個月的總天數(shù) agoDay = max + agoDay;//天數(shù)在上個月的總天數(shù)的基礎(chǔ)上減去負(fù)數(shù) } break; } //3.0對處理的年月日作邏輯判斷 //如果beginDay > max(如果是當(dāng)前時間的天數(shù)+1后的數(shù)值超過了上個月的總天數(shù): 天數(shù)變?yōu)?,月份增加1) if (agoDay > max) { agoDay = 1; agoMoon += 1; } //如果月份當(dāng)月為1月的時候, 那么一個月內(nèi): 年:-1 月:12 日:依然不變 if (agoMoon == 0) { agoMoon = 12; agoYear = currentYear - 1; } //4.0對已經(jīng)處理好的數(shù)據(jù)作格式處理(單位數(shù)則自動補(bǔ)零) currentMoon = Appendzero(currentMoon); currentDay = Appendzero(currentDay); agoMoon = Appendzero(agoMoon); agoDay = Appendzero(agoDay); //5.0幫助代碼 console.log("當(dāng)前時間為:{0}-{1}-{2}".format(currentYear, currentMoon, currentDay)); console.log("一個月前的時間為{0}-{1}-{2}".format(agoYear, agoMoon, agoDay)); return { "Year": agoYear, "Moon": agoMoon, "Day": agoDay }; } //處理各位數(shù)為零的數(shù)字(單位數(shù)則加0) function Appendzero(obj) { if (obj < 10) { return "0" + obj; } else { return obj; } } </script> </head> <body> <input type="button" class="tbBtn" id="CbtnNull" style="background-color:#e3e3e3" value="不限"/> <input type="button" class="tbBtn" id="CbtnMoon" style="width: 80px; margin-left: 5px; margin-right: 5px;" value="一個月內(nèi)"/> <input type="button" class="tbBtn" id="CbtnWeek" style="width: 80px; margin-left: 5px; margin-right: 5px;" value="一周內(nèi)"/> <input id = "txtCallCycleBegin" type="text"/> <input id = "txtCallCyclecurrend" type="text"/> </body> </html>
以上就是本文的全部內(nèi)容,希望能夠幫助大家更好的解決javascript日期處理問題。
- js 取時間差去掉周六周日實(shí)現(xiàn)代碼
- js獲取當(dāng)前周、上一周、下一周日期
- js獲取時間(本周、本季度、本月..)
- javascript 當(dāng)前日期加(天、周、月、年)
- js實(shí)現(xiàn)獲取當(dāng)前時間是本月第幾周的方法
- JS獲取月份最后天數(shù)、最大天數(shù)與某日周數(shù)的方法
- JS 操作日期 順便實(shí)現(xiàn) 上一周 和 下一周 功能
- JavaScript 計(jì)算當(dāng)天是本年本月的第幾周
- JS獲取本周周一,周末及獲取任意時間的周一周末功能示例
- js輸出陰歷、陽歷、年份、月份、周示例代碼
- JS實(shí)現(xiàn)獲取當(dāng)前所在周的周六、周日示例分析
相關(guān)文章
JS 非圖片動態(tài)loading效果實(shí)現(xiàn)代碼
功能說明:譬如在按某個button時,顯示消息"Loading”,然后每隔一秒后后面加上".",至一定數(shù)量的"."時如:"Loading...",再重置此消息為"Loading",繼續(xù)動態(tài)顯示,直至按鈕事件處理完成。2010-04-04hash特點(diǎn)、hashchange事件介紹及其常見應(yīng)用場景
淺析hash特點(diǎn)、hashchange事件介紹及其常見應(yīng)用場景(不同hash對應(yīng)不同事件處理、移動端大圖展示狀態(tài)后退頁面問題、原生輕應(yīng)用頭部后退問題、移動端自帶返回按鈕二次確認(rèn)問題),hashchange和popstate事件觸發(fā)條件2023-11-11javascript數(shù)組中的reduce方法和pop方法
這篇文章主要介紹了javascript數(shù)組中的reduce方法和pop方法,文章內(nèi)容介紹詳細(xì),具有一定的參考價值需要的小伙伴可以參考一下,希望對你的學(xué)習(xí)有所幫助2022-03-03JavaScript Event Loop相關(guān)原理解析
這篇文章主要介紹了JavaScript Event Loop相關(guān)原理解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06jQuery?事件綁定及取消?bind?live?delegate?on?one區(qū)別解析
這篇文章主要介紹了jquery?事件綁定及取消?bind?live?delegate?on?one區(qū)別解析,本文給大家講解的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-11-11