javascript顯示上周、上個(gè)月日期的處理方法
本文實(shí)例介紹了javascript一周前、一個(gè)月前的實(shí)現(xiàn)代碼,對(duì)于javascript日期處理進(jìn)行了簡(jiǎ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ā) }) //專(zhuān)門(mén)包裝點(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ù),返回一個(gè)字面量; function ProcessDate(type) { //1.0獲取現(xiàn)在時(shí)間的年月日: var currentTime = new Date("2016-01-02"); //得到當(dāng)前的時(shí)間 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)前時(shí)間的一個(gè)月內(nèi)的年月日:(一個(gè)月內(nèi)的大眾業(yè)務(wù)需求為:當(dāng)前時(shí)間的月份-1,當(dāng)前時(shí)間的天數(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(); //獲取上個(gè)月的總天數(shù) break; case 7: agoDay = currentDay - 6; if (agoDay < 0) { agoMoon = currentMoon - 1;//月份減1 max = new Date(agoYear, agoMoon, 0).getDate(); //獲取上個(gè)月的總天數(shù) agoDay = max + agoDay;//天數(shù)在上個(gè)月的總天數(shù)的基礎(chǔ)上減去負(fù)數(shù) } break; } //3.0對(duì)處理的年月日作邏輯判斷 //如果beginDay > max(如果是當(dāng)前時(shí)間的天數(shù)+1后的數(shù)值超過(guò)了上個(gè)月的總天數(shù): 天數(shù)變?yōu)?,月份增加1) if (agoDay > max) { agoDay = 1; agoMoon += 1; } //如果月份當(dāng)月為1月的時(shí)候, 那么一個(gè)月內(nèi): 年:-1 月:12 日:依然不變 if (agoMoon == 0) { agoMoon = 12; agoYear = currentYear - 1; } //4.0對(duì)已經(jīng)處理好的數(shù)據(jù)作格式處理(單位數(shù)則自動(dòng)補(bǔ)零) currentMoon = Appendzero(currentMoon); currentDay = Appendzero(currentDay); agoMoon = Appendzero(agoMoon); agoDay = Appendzero(agoDay); //5.0幫助代碼 console.log("當(dāng)前時(shí)間為:{0}-{1}-{2}".format(currentYear, currentMoon, currentDay)); console.log("一個(gè)月前的時(shí)間為{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="一個(gè)月內(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>
以上就是本文的全部?jī)?nèi)容,希望能夠幫助大家更好的解決javascript日期處理問(wèn)題。
- js 取時(shí)間差去掉周六周日實(shí)現(xiàn)代碼
- js獲取當(dāng)前周、上一周、下一周日期
- js獲取時(shí)間(本周、本季度、本月..)
- javascript 當(dāng)前日期加(天、周、月、年)
- js實(shí)現(xiàn)獲取當(dāng)前時(shí)間是本月第幾周的方法
- JS獲取月份最后天數(shù)、最大天數(shù)與某日周數(shù)的方法
- JS 操作日期 順便實(shí)現(xiàn) 上一周 和 下一周 功能
- JavaScript 計(jì)算當(dāng)天是本年本月的第幾周
- JS獲取本周周一,周末及獲取任意時(shí)間的周一周末功能示例
- js輸出陰歷、陽(yáng)歷、年份、月份、周示例代碼
- JS實(shí)現(xiàn)獲取當(dāng)前所在周的周六、周日示例分析
相關(guān)文章
JS 非圖片動(dòng)態(tài)loading效果實(shí)現(xiàn)代碼
功能說(shuō)明:譬如在按某個(gè)button時(shí),顯示消息"Loading”,然后每隔一秒后后面加上".",至一定數(shù)量的"."時(shí)如:"Loading...",再重置此消息為"Loading",繼續(xù)動(dòng)態(tài)顯示,直至按鈕事件處理完成。2010-04-04微信小程序?qū)崿F(xiàn)簡(jiǎn)易的計(jì)算器功能
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)簡(jiǎn)易的計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09hash特點(diǎn)、hashchange事件介紹及其常見(jiàn)應(yīng)用場(chǎng)景
淺析hash特點(diǎn)、hashchange事件介紹及其常見(jiàn)應(yīng)用場(chǎng)景(不同hash對(duì)應(yīng)不同事件處理、移動(dòng)端大圖展示狀態(tài)后退頁(yè)面問(wèn)題、原生輕應(yīng)用頭部后退問(wèn)題、移動(dòng)端自帶返回按鈕二次確認(rèn)問(wèn)題),hashchange和popstate事件觸發(fā)條件2023-11-11詳解JavaScript基本類(lèi)型和引用類(lèi)型
這篇文章主要介紹了JavaScript基本類(lèi)型和引用類(lèi)型,基本類(lèi)型中還包含了類(lèi)型轉(zhuǎn)換,感興趣的小伙伴們可以參考一下2015-12-12javascript數(shù)組中的reduce方法和pop方法
這篇文章主要介紹了javascript數(shù)組中的reduce方法和pop方法,文章內(nèi)容介紹詳細(xì),具有一定的參考價(jià)值需要的小伙伴可以參考一下,希望對(duì)你的學(xué)習(xí)有所幫助2022-03-03JavaScript日期和時(shí)間的格式化及其它常用處理方法
這篇文章主要給大家介紹了關(guān)于JavaScript日期和時(shí)間的格式化及其它常用處理方法,JavaScript中可以使用Date對(duì)象來(lái)表示日期和時(shí)間,如果需要格式化日期和時(shí)間,可以使用Date對(duì)象的幾個(gè)方法和一些字符串操作方法來(lái)實(shí)現(xiàn),需要的朋友可以參考下2023-09-09JavaScript Event Loop相關(guān)原理解析
這篇文章主要介紹了JavaScript Event Loop相關(guān)原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06jQuery?事件綁定及取消?bind?live?delegate?on?one區(qū)別解析
這篇文章主要介紹了jquery?事件綁定及取消?bind?live?delegate?on?one區(qū)別解析,本文給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-11-11