Javascript: Date scheduling feature implementation

Source: Internet
Author: User

Background:

Recently, the company's products often encounter date scheduling similar functions;

The scheduling dates for the requirements vary: some are two weeks, some are four weeks, there is a status of active when the check is selected, and a hook gets the shape of the selected date: "2018-04-11" data in this format.

Realize:

                */* Get schedule for the day and after * @param daycount: Relative to today's days, the shape of: 0,1,2 ....            */function Getdatedata (daycount) {var d = new Date ();            D.setdate (d.getdate () + Daycount);            var y = d.getfullyear (); var m = (D.getmonth () + 1) > 10?            D.getmonth () + 1: ' 0 ' + (d.getmonth () + 1); var d = d.getdate () > 10?            D.getdate (): ' 0 ' + d.getdate ();        Return y + "-" + M + "-" + D; }/* * Gets the day of the week name * @param Date: Date to convert: ' 2018-04-11 ' or ' 2018-4-11 ' */function dat            Etoday (date) {var dayno = new Date (date). GetDay ();                    Switch (dayno) {case 0:return ' Sunday ';                Break                    Case 1:return ' Monday ';                Break                    Case 2:return ' Tuesday ';                Break              Case 3:return ' Wednesday ';      Break                    Case 4:return ' Thursday ';                Break                    Case 5:return ' Friday ';                Break                    Case 6:return ' Saturday ';                Break            Default:break; }/* * Dynamically get Schedule Date by parameter * @param weekcount: Number of weeks, int * @param domid: Output Domid *                /function Initweekdata (Weekcount, Domid) {var Weekdatetempl = ', Tabletempl = ',                Tableth = ", tabletd =", Tabletdarr = [], weekdata = [],            Weekdatafinal = [], Weekdateele;            for (var i = 0; i < Weekcount * 7; i++) {Weekdata[i] = Getdatedata (i); } for (var i = 0; i < weekdata.length; i = i + 7) {Weekdatafinal.push (Weekdata.slice (i, i +            7));      }      Weekdatafinal.foreach (function (item, index) {if (index = = 0) {for (var i = 0; i < Item.length;                    i++) {Tableth + = ' <th> ' + datetoday (item[i]) + ' </th> '}                    Tableth = ' <tr> ' + tableth + ' </tr> ';                            Firstweek for (var j = 0; J < Item.length; J + +) {if (j = = 0) {                        tabletd + = ' <td attr-date= ' + item[j] + ' class= ' active ' > Today </td> '; } else {tabletd + = ' <td attr-date= ' + item[j] + ' > ' + new Date (Item[j]). GetDate () + ' &lt                        ;/td> ';                    }} tabletd = ' <tr> ' + tabletd + ' </tr> ';                    Tabletdarr[index] = tabletd;                tabletd = ";    } else {for (var k = 0; k < item.length; k++) {                    tabletd + = ' <td attr-date= ' + item[k] + ' > ' + new Date (Item[k]). GetDate () + ' </td> ';                    } tabletd = ' <tr> ' + tabletd + ' </tr> ';                    Tabletdarr[index] = tabletd;                tabletd = ";            }            });            Tabletempl = Tableth + Tabletdarr.join (");            Weekdateele = Document.queryselector (' # ' + domid);            weekdateele.innerhtml = Tabletempl; Weekdateele.queryselectorall (' Tr>td '). ForEach (function (item, index) {item.addeventlistener ("click", Fun                        Ction () {weekdateele.queryselectorall (' tr>td '). ForEach (function (item, index) {                    Item.classList.remove (' active ');                    });                    This.classList.add (' active ');            Alert (This.getattribute (' Attr-date ')});        }); }

  

Invoke instance:

   Call Instance   Initweekdata (2, ' twoweekdate ');   Initweekdata (4, ' fourweekdate ');

Implementation results:

On-line Demo:

Https://codepen.io/kevinInsight/pen/mxoOaz

Javascript: Date scheduling feature implementation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.