javascript displays processing methods for last week and last month dates

  • 2020-12-18 01:44:21
  • OfStack

This article introduces javascript1 week ago, 1 month ago implementation code, javascript date processing for a simple analysis, shared for your reference, the specific content is as follows


<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();// Click event trigger 
  })

  // Specially packaged click events; 
  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()));
    }
   })
  }

  // Function that handles dates, returns 1 A literal; 
  function ProcessDate(type) {
   //1.0 Get the year, month and day of the present time: 
   var currentTime = new Date("2016-01-02"); // Get the current time 
   var currentYear = currentTime.getFullYear(); // Get the current year 
   var currentMoon = currentTime.getMonth() + 1; // Gets the current month (default is 0-11 So I'm going to add 1 Is the current month) 
   var currentDay = currentTime.getDate(); // Gets the current number of days 

   //2.0 Gets the current time 1 Within a month :( 1 Mass business requirements for the month: Month of the current time -1 , the number of days at the current time +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(); // Gets the total number of days last month 
     break;
    case 7:
     agoDay = currentDay - 6;
     if (agoDay < 0) {
      agoMoon = currentMoon - 1;// Reduction in 1
      max = new Date(agoYear, agoMoon, 0).getDate(); // Gets the total number of days last month 
      agoDay = max + agoDay;// The number of days is subtracted from the total number of days in the previous month 
     }
     break;
   }

   //3.0 Make logical judgments about the year, month and day of processing 


   // if beginDay > max If it is the number of days at the current time +1 The following value exceeds the total number of days in the previous month:   Number of days to 1 , monthly increase 1 ) 
   if (agoDay > max) {
    agoDay = 1;
    agoMoon += 1;
   }

   // If month month is 1 At the time of the month,   then 1 Months:   Years: -1  Month: 12  Date: same as before  
   if (agoMoon == 0) {
    agoMoon = 12;
    agoYear = currentYear - 1;
   }

   //4.0 Format the processed data (the number of units is automatically zero) 
   currentMoon = Appendzero(currentMoon);
   currentDay = Appendzero(currentDay);
   agoMoon = Appendzero(agoMoon);
   agoDay = Appendzero(agoDay);

   //5.0 Help code 
   console.log(" The current time is: {0}-{1}-{2}".format(currentYear, currentMoon, currentDay));
   console.log("1 A month ago the time was zero {0}-{1}-{2}".format(agoYear, agoMoon, agoDay));

   return { "Year": agoYear, "Moon": agoMoon, "Day": agoDay };
  }

  // Handle numbers with zero bits (plus units) 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=" There is no limit "/>
 <input type="button" class="tbBtn" id="CbtnMoon" style="width: 80px; margin-left: 5px; margin-right: 5px;" value="1 months "/>
 <input type="button" class="tbBtn" id="CbtnWeek" style="width: 80px; margin-left: 5px; margin-right: 5px;" value="1 weeks "/>
 <input id = "txtCallCycleBegin" type="text"/>
 <input id = "txtCallCyclecurrend" type="text"/>
</body>
</html>

That's the end of this article, and hopefully it will help you solve the javascript date handling problem better.


Related articles: