Best JavaScript code snippet using best
calender.js
Source:calender.js
1 var date_set = [];2 var current_table_caption = null;3 var previous_selected = null;4 function setAttributes(){5 current_table_caption = document.getElementsByClassName('title')[0].innerHTML6 buttons_div = document.getElementById('buttons');7 buttons_div.innerHTML+= "<button onmousedown='nextMonth();' id='next' class='button navButton'><span>>|</span></button>";8 buttons_div.innerHTML+= "<button onmousedown='previousMonth();' id='previous' class='button navButton'><span>|<</span></button>";9 finish_button = document.getElementById('nextButton');10 finish_button.innerHTML = "<span>Finish</span>"11 }12 function addDate(set_date) {13 if (set_date == '' || set_date == null) 14 return 15 16 if ((new Date(set_date)) <= sessionDate) {17 showMessage("Selected date is less than set current clinic date");18 return 19 }20 td = document.getElementById(set_date); 21 if (td.style.background.match(/tomato/i) && !previous_selected == set_date) { 22 td.style.background = '#CADCEA'; 23 document.getElementById("appointment_date").value = null;24 }else{ 25 try { 26 td = document.getElementById(previous_selected); 27 td.style.background = '#CADCEA'; 28 }catch(e) {}29 td = document.getElementById(set_date); 30 td.style.background = 'tomato'; 31 previous_selected = set_date;32 document.getElementById("appointment_date").value = set_date; 33 showRecordedAppointments(set_date); 34 showDate(); 35 } 36 try { 37 set_emr_date = document.getElementById(setNextAppointmentDate); 38 if (!set_emr_date.style.background.match(/tomato/i)) { 39 set_emr_date.style.background = 'lightyellow'; 40 } 41 }catch(e) {}42 }43 function removeDate(set_date) {44 dates = date_set ; date_set = []45 for (i = 0 ; i < dates.length ; i++) {46 if (dates[i] != set_date)47 date_set.push(dates[i])48 }49 }50 function previousMonth(){51 if (current_table_caption == 'January') { 52 currYear = parseFloat(document.getElementById("app_date").innerHTML) - 1 53 document.getElementById("app_date").innerHTML = currYear ; 54 setDate = new Date("1/1/" + currYear); 55 chart(); 56 document.getElementById("app_date").innerHTML = currYear; 57 58 while (current_table_caption != "December") { 59 nextMonth(); 60 }61 } else if (current_table_caption == 'December') {62 month = document.getElementById(current_table_caption)63 month.style.display = 'none';64 next_month = document.getElementById('November')65 next_month.style.display = 'inline';66 current_table_caption = next_month.id67 } else if (current_table_caption == 'November') {68 month = document.getElementById(current_table_caption)69 month.style.display = 'none';70 next_month = document.getElementById('October')71 next_month.style.display = 'inline';72 current_table_caption = next_month.id73 } else if (current_table_caption == 'October') {74 month = document.getElementById(current_table_caption)75 month.style.display = 'none';76 next_month = document.getElementById('September')77 next_month.style.display = 'inline';78 current_table_caption = next_month.id79 } else if (current_table_caption == 'September') {80 month = document.getElementById(current_table_caption)81 month.style.display = 'none';82 next_month = document.getElementById('August')83 next_month.style.display = 'inline';84 current_table_caption = next_month.id85 } else if (current_table_caption == 'August') {86 month = document.getElementById(current_table_caption)87 month.style.display = 'none';88 next_month = document.getElementById('July')89 next_month.style.display = 'inline';90 current_table_caption = next_month.id91 } else if (current_table_caption == 'July') {92 month = document.getElementById(current_table_caption)93 month.style.display = 'none';94 next_month = document.getElementById('June')95 next_month.style.display = 'inline';96 current_table_caption = next_month.id97 } else if (current_table_caption == 'June') {98 month = document.getElementById(current_table_caption)99 month.style.display = 'none';100 next_month = document.getElementById('May')101 next_month.style.display = 'inline';102 current_table_caption = next_month.id103 } else if (current_table_caption == 'May') {104 month = document.getElementById(current_table_caption)105 month.style.display = 'none';106 next_month = document.getElementById('April')107 next_month.style.display = 'inline';108 current_table_caption = next_month.id109 } else if (current_table_caption == 'April') {110 month = document.getElementById(current_table_caption)111 month.style.display = 'none';112 next_month = document.getElementById('March')113 next_month.style.display = 'inline';114 current_table_caption = next_month.id115 } else if (current_table_caption == 'March') {116 month = document.getElementById(current_table_caption)117 month.style.display = 'none';118 next_month = document.getElementById('February')119 next_month.style.display = 'inline';120 current_table_caption = next_month.id121 } else if (current_table_caption == 'February') {122 month = document.getElementById(current_table_caption)123 month.style.display = 'none';124 next_month = document.getElementById('January')125 next_month.style.display = 'inline';126 current_table_caption = next_month.id;127 }128 msgBox = document.getElementById('information'); 129 year = document.getElementById('app_date').innerHTML; 130 msgBox.innerHTML = "<span id ='app_date'>" + year + "</span> Total number of booked patients on this day: " + 0;131 try { 132 td = document.getElementById(setNextAppointmentDate); 133 td.style.background = "lightyellow"; 134 } catch(e) {}135 try { 136 td = document.getElementById(previous_selected); 137 td.style.background = "tomato"; 138 } catch(e) {}139 }140 function nextMonth(){141 if (current_table_caption == 'January') {142 month = document.getElementById(current_table_caption)143 month.style.display = 'none';144 next_month = document.getElementById('February')145 next_month.style.display = 'inline';146 current_table_caption = next_month.id147 } else if (current_table_caption == 'February') {148 month = document.getElementById(current_table_caption)149 month.style.display = 'none';150 next_month = document.getElementById('March')151 next_month.style.display = 'inline';152 current_table_caption = next_month.id153 } else if (current_table_caption == 'March') {154 month = document.getElementById(current_table_caption)155 month.style.display = 'none';156 next_month = document.getElementById('April')157 next_month.style.display = 'inline';158 current_table_caption = next_month.id159 } else if (current_table_caption == 'April') {160 month = document.getElementById(current_table_caption)161 month.style.display = 'none';162 next_month = document.getElementById('May')163 next_month.style.display = 'inline';164 current_table_caption = next_month.id165 } else if (current_table_caption == 'May') {166 month = document.getElementById(current_table_caption)167 month.style.display = 'none';168 next_month = document.getElementById('June')169 next_month.style.display = 'inline';170 current_table_caption = next_month.id171 } else if (current_table_caption == 'June') {172 month = document.getElementById(current_table_caption)173 month.style.display = 'none';174 next_month = document.getElementById('July')175 next_month.style.display = 'inline';176 current_table_caption = next_month.id177 } else if (current_table_caption == 'July') {178 month = document.getElementById(current_table_caption)179 month.style.display = 'none';180 next_month = document.getElementById('August')181 next_month.style.display = 'inline';182 current_table_caption = next_month.id183 } else if (current_table_caption == 'August') {184 month = document.getElementById(current_table_caption)185 month.style.display = 'none';186 next_month = document.getElementById('September')187 next_month.style.display = 'inline';188 current_table_caption = next_month.id189 } else if (current_table_caption == 'September') {190 month = document.getElementById(current_table_caption)191 month.style.display = 'none';192 next_month = document.getElementById('October')193 next_month.style.display = 'inline';194 current_table_caption = next_month.id195 } else if (current_table_caption == 'October') {196 month = document.getElementById(current_table_caption)197 month.style.display = 'none';198 next_month = document.getElementById('November')199 next_month.style.display = 'inline';200 current_table_caption = next_month.id201 } else if (current_table_caption == 'November') {202 month = document.getElementById(current_table_caption)203 month.style.display = 'none';204 next_month = document.getElementById('December')205 next_month.style.display = 'inline';206 current_table_caption = next_month.id207 } else if (current_table_caption == 'December') { 208 current_table_caption = "January"; 209 currYear = parseFloat(document.getElementById("app_date").innerHTML) + 1 210 document.getElementById("app_date").innerHTML = currYear 211 setDate = new Date("1/1/" + currYear); 212 chart(); 213 try { 214 document.getElementById("year").innerHTML = currYear; 215 } catch(e) {} 216 } 217 msgBox = document.getElementById('information'); 218 year = document.getElementById('app_date').innerHTML; 219 msgBox.innerHTML = "<span id ='app_date'>" + year + "</span> Total number of booked patients on this day: " + 0;220 221 try { 222 td = document.getElementById(setNextAppointmentDate); 223 td.style.background = "lightyellow";224 } catch(e) {}225 try { 226 td = document.getElementById(previous_selected); 227 td.style.background = "tomato"; 228 } catch(e) {}229 }230 function setAppointmentDate(){231 return232 selected_dates = "<%= @clinic_holidays %>"233 if (selected_dates != ''){234 change_attribute = 0;235 selected_dates = selected_dates.split(',')236 for(i = 0 ; i < selected_dates.length ; i++){237 for(x = 0 ; x < date_set.length ; x++){238 if(selected_dates[i].substring(5,10) == date_set[x].substring(5,10)){239 change_attribute+= 1;240 }241 }242 }243 if (change_attribute == selected_dates.length && change_attribute == date_set.length){244 document.location = "/clinic";245 return;246 }247 }248 submitForm = document.createElement("FORM");249 submitForm.setAttribute("type","hidden");250 document.body.appendChild(submitForm);251 submitForm.method = "POST";252 newElement = document.createElement("input")253 newElement.setAttribute("name",'holidays')254 newElement.setAttribute("type","hidden");255 submitForm.appendChild(newElement);256 newElement.value = date_set.join(',');257 submitForm.action= "/properties/create_clinic_holidays";258 submitForm.submit();259 }260 function daysInMonth(month,year) {261 var m = [31,28,31,30,31,30,31,31,30,31,30,31];262 if (month != 2) return m[month - 1];263 if (year%4 != 0) return m[1];264 if (year%100 == 0 && year%400 != 0) return m[1];265 return m[1] + 1;266 } 267 function currMonth(month_num) { 268 var month = new Array(12); 269 month[0]="January"; 270 month[1]="February"; 271 month[2]="March"; 272 month[3]="April"; 273 month[4]="May"; 274 month[5]="June"; 275 month[6]="July"; 276 month[7]="August"; 277 month[8]="September"; 278 month[9]="October"; 279 month[10]="November"; 280 month[11]="December"; 281 282 return month[month_num]; 283 }284 function chart() {285 nextAppointmentDate = new Date(setDate);286 var chart = ''287 var container = "<div class = 'container'>\n"288 var number = 1289 while (number < 13) {290 var startDate = number + "/1/" + nextAppointmentDate.getFullYear();291 startDate = new Date(startDate);292 var daysIn = daysInMonth((startDate.getMonth() + 1) , startDate.getFullYear());293 var endDate = number + "/" + daysIn + "/" + nextAppointmentDate.getFullYear();294 number++;295 endDate = new Date(endDate);296 chart+="<table id='" + currMonth(endDate.getMonth()) + "' class='months'>";297 chart+="\n<caption class = 'title'>" + currMonth(endDate.getMonth()) + "</caption>"298 chart+="\n<tr>\n<th>Sunday</th>\n<th>Monday</th>\n<th>Tuesday</th>\n<th>Wednesday</th>"299 chart+="\n<th>Thursday</th>\n<th>Friday</th>\n<th>Saturday</th>\n</tr>"300 301 while (startDate <= endDate) { 302 var sunday = '' ; var monday = '' ; var tuesday = '';303 var wednesday = '' ; var thursday = ''; 304 var friday = '' ; var saturday = '';305 var day = startDate.getDay();306 var wkDays = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"];307 day = wkDays[day];308 if (day == 'Monday') {309 monday = startDate.getDate(); 310 }else if (day == 'Tuesday') {311 tuesday = startDate.getDate(); 312 }else if (day == 'Wednesday') { 313 wednesday = startDate.getDate(); 314 }else if (day == 'Thursday') { 315 thursday = startDate.getDate(); 316 }else if (day == 'Friday') { 317 friday = startDate.getDate(); 318 }else if (day == 'Saturday') { 319 saturday = startDate.getDate(); 320 }else if (day == 'Sunday') { 321 sunday = startDate.getDate(); 322 }323 try {324 if (monday) {325 if (new Date((startDate.getMonth() + 1) + "/" + (startDate.getDate() + 1) + "/" + startDate.getFullYear()) <= endDate) {326 startDate = (new Date((startDate.getMonth() + 1) + "/" + (startDate.getDate() + 1) + "/" + startDate.getFullYear()))327 tuesday = startDate.getDate()328 }329 if (new Date((startDate.getMonth() + 1) + "/" + (startDate.getDate() + 1) + "/" + startDate.getFullYear()) <= endDate) {330 startDate = (new Date((startDate.getMonth() + 1) + "/" + (startDate.getDate() + 1) + "/" + startDate.getFullYear()))331 wednesday = startDate.getDate()332 }333 if (new Date((startDate.getMonth() + 1) + "/" + (startDate.getDate() + 1) + "/" + startDate.getFullYear()) <= endDate) {334 startDate = (new Date((startDate.getMonth() + 1) + "/" + (startDate.getDate() + 1) + "/" + startDate.getFullYear()))335 thursday = startDate.getDate()336 }337 if (new Date((startDate.getMonth() + 1) + "/" + (startDate.getDate() + 1) + "/" + startDate.getFullYear()) <= endDate) {338 startDate = (new Date((startDate.getMonth() + 1) + "/" + (startDate.getDate() + 1) + "/" + startDate.getFullYear()))339 friday = startDate.getDate()340 }341 if (new Date((startDate.getMonth() + 1) + "/" + (startDate.getDate() + 1) + "/" + startDate.getFullYear()) <= endDate) {342 startDate = (new Date((startDate.getMonth() + 1) + "/" + (startDate.getDate() + 1) + "/" + startDate.getFullYear()))343 saturday = startDate.getDate()344 }345 } else if (tuesday) {346 if (new Date((startDate.getMonth() + 1) + "/" + (startDate.getDate() + 1) + "/" + startDate.getFullYear()) <= endDate) {347 startDate = (new Date((startDate.getMonth() + 1) + "/" + (startDate.getDate() + 1) + "/" + startDate.getFullYear()))348 wednesday = startDate.getDate()349 }350 if (new Date((startDate.getMonth() + 1) + "/" + (startDate.getDate() + 1) + "/" + startDate.getFullYear()) <= endDate) {351 startDate = (new Date((startDate.getMonth() + 1) + "/" + (startDate.getDate() + 1) + "/" + startDate.getFullYear())) 352 thursday = startDate.getDate()353 } 354 if (new Date((startDate.getMonth() + 1) + "/" + (startDate.getDate() + 1) + "/" + startDate.getFullYear()) <= endDate) {355 startDate = (new Date((startDate.getMonth() + 1) + "/" + (startDate.getDate() + 1) + "/" + startDate.getFullYear()))356 friday = startDate.getDate()357 } 358 if (new Date((startDate.getMonth() + 1) + "/" + (startDate.getDate() + 1) + "/" + startDate.getFullYear()) <= endDate) {359 startDate = (new Date((startDate.getMonth() + 1) + "/" + (startDate.getDate() + 1) + "/" + startDate.getFullYear()))360 saturday = startDate.getDate()361 }362 } else if (wednesday) {363 if (new Date((startDate.getMonth() + 1) + "/" + (startDate.getDate() + 1) + "/" + startDate.getFullYear()) <= endDate) {364 startDate = (new Date((startDate.getMonth() + 1) + "/" + (startDate.getDate() + 1) + "/" + startDate.getFullYear()))365 thursday = startDate.getDate()366 }367 if (new Date((startDate.getMonth() + 1) + "/" + (startDate.getDate() + 1) + "/" + startDate.getFullYear()) <= endDate) {368 startDate = (new Date((startDate.getMonth() + 1) + "/" + (startDate.getDate() + 1) + "/" + startDate.getFullYear()))369 friday = startDate.getDate()370 }371 if (new Date((startDate.getMonth() + 1) + "/" + (startDate.getDate() + 1) + "/" + startDate.getFullYear()) <= endDate) {372 startDate = (new Date((startDate.getMonth() + 1) + "/" + (startDate.getDate() + 1) + "/" + startDate.getFullYear()))373 saturday = startDate.getDate()374 }375 } else if (thursday) {376 if (new Date((startDate.getMonth() + 1) + "/" + (startDate.getDate() + 1) + "/" + startDate.getFullYear()) <= endDate) {377 startDate = (new Date((startDate.getMonth() + 1) + "/" + (startDate.getDate() + 1) + "/" + startDate.getFullYear()))378 friday = startDate.getDate()379 }380 if (new Date((startDate.getMonth() + 1) + "/" + (startDate.getDate() + 1) + "/" + startDate.getFullYear()) <= endDate) {381 startDate = (new Date((startDate.getMonth() + 1) + "/" + (startDate.getDate() + 1) + "/" + startDate.getFullYear()))382 saturday = startDate.getDate()383 }384 } else if (friday) {385 if (new Date((startDate.getMonth() + 1) + "/" + (startDate.getDate() + 1) + "/" + startDate.getFullYear()) <= endDate) {386 startDate = (new Date((startDate.getMonth() + 1) + "/" + (startDate.getDate() + 1) + "/" + startDate.getFullYear()))387 saturday = startDate.getDate()388 }389 } else if (sunday) {390 if (new Date((startDate.getMonth() + 1) + "/" + (startDate.getDate() + 1) + "/" + startDate.getFullYear()) <= endDate) {391 startDate = (new Date((startDate.getMonth() + 1) + "/" + (startDate.getDate() + 1) + "/" + startDate.getFullYear()))392 monday = startDate.getDate()393 }394 if (new Date((startDate.getMonth() + 1) + "/" + (startDate.getDate() + 1) + "/" + startDate.getFullYear()) <= endDate) {395 startDate = (new Date((startDate.getMonth() + 1) + "/" + (startDate.getDate() + 1) + "/" + startDate.getFullYear()))396 tuesday = startDate.getDate()397 }398 if (new Date((startDate.getMonth() + 1) + "/" + (startDate.getDate() + 1) + "/" + startDate.getFullYear()) <= endDate) {399 startDate = (new Date((startDate.getMonth() + 1) + "/" + (startDate.getDate() + 1) + "/" + startDate.getFullYear()))400 wednesday = startDate.getDate()401 }402 if (new Date((startDate.getMonth() + 1) + "/" + (startDate.getDate() + 1) + "/" + startDate.getFullYear()) <= endDate) {403 startDate = (new Date((startDate.getMonth() + 1) + "/" + (startDate.getDate() + 1) + "/" + startDate.getFullYear()))404 thursday = startDate.getDate()405 }406 if (new Date((startDate.getMonth() + 1) + "/" + (startDate.getDate() + 1) + "/" + startDate.getFullYear()) <= endDate) {407 startDate = (new Date((startDate.getMonth() + 1) + "/" + (startDate.getDate() + 1) + "/" + startDate.getFullYear()))408 friday = startDate.getDate()409 }410 if (new Date((startDate.getMonth() + 1) + "/" + (startDate.getDate() + 1) + "/" + startDate.getFullYear()) <= endDate) {411 startDate = (new Date((startDate.getMonth() + 1) + "/" + (startDate.getDate() + 1) + "/" + startDate.getFullYear()))412 saturday = startDate.getDate()413 }414 } 415 }catch(e) {}416 417 try{418 sunday_str = new Date((startDate.getMonth() + 1) + "/" + sunday + "/" + startDate.getFullYear())419 sunday_str = dateFormat(sunday_str,"yyyy-mm-dd");420 }catch(e) {sunday_str = ''}421 try{422 monday_str = new Date((startDate.getMonth() + 1) + "/" + monday + "/" + startDate.getFullYear())423 monday_str = dateFormat(monday_str,"yyyy-mm-dd");424 }catch(e) {monday_str = ''}425 try{426 tuesday_str = new Date((startDate.getMonth() + 1) + "/" + tuesday + "/" + startDate.getFullYear())427 tuesday_str = dateFormat(tuesday_str,"yyyy-mm-dd");428 }catch(e) {tuesday_str = ''}429 try{430 wednesday_str = new Date((startDate.getMonth() + 1) + "/" + wednesday + "/" + startDate.getFullYear())431 wednesday_str = dateFormat(wednesday_str,"yyyy-mm-dd");432 }catch(e) {wednesday_str = ''}433 try{434 thursday_str = new Date((startDate.getMonth() + 1) + "/" + thursday + "/" + startDate.getFullYear())435 thursday_str = dateFormat(thursday_str,"yyyy-mm-dd");436 }catch(e) {thursday_str = ''}437 try{438 friday_str = new Date((startDate.getMonth() + 1) + "/" + friday + "/" + startDate.getFullYear())439 friday_str = dateFormat(friday_str,"yyyy-mm-dd");440 }catch(e) {friday_str = ''}441 try{442 saturday_str = new Date((startDate.getMonth() + 1) + "/" + saturday + "/" + startDate.getFullYear())443 saturday_str = dateFormat(saturday_str,"yyyy-mm-dd");444 }catch(e) {saturday_str = ''}445 chart+="\n<tr>"446 chart+= '\n<td onMouseDown="addDate(\''+sunday_str+'\');" class="dates" id="'+sunday_str+'">' +sunday+ "</td>";447 chart+= '\n<td onMouseDown="addDate(\''+monday_str+'\');" class="dates" id="'+monday_str+'">' +monday+ "</td>";448 chart+= '\n<td onMouseDown="addDate(\''+tuesday_str+'\');" class="dates" id="'+tuesday_str+'">' +tuesday+ "</td>";449 chart+= '\n<td onMouseDown="addDate(\''+wednesday_str+'\');" class="dates" id="'+wednesday_str+'">' +wednesday+ "</td>";450 chart+= '\n<td onMouseDown="addDate(\''+thursday_str+'\');" class="dates" id="'+thursday_str+'">' +thursday+ "</td>";451 chart+= '\n<td onMouseDown="addDate(\''+friday_str+'\');" class="dates" id="'+friday_str+'">' +friday+ "</td>";452 chart+= '\n<td onMouseDown="addDate(\''+saturday_str+'\');" class="dates" id="'+saturday_str+'">' +saturday+ "</td>";453 chart+="\n</tr>"454 try {455 startDate = (new Date((startDate.getMonth() + 1) + "/" + (startDate.getDate() + 1) + "/" + startDate.getFullYear()))456 }catch(e){457 break; 458 }459 }460 chart+='\n</table>'461 }462 container += chart + "\n</div><br />";463 element = document.getElementsByClassName("inputFrameClass")[0];464 element.innerHTML = container;465 element.style.cssText = "611px !important";466 }467//............................................................................468 function showRecordedAppointments(setdate) { 469 var msgBox = $('information'); 470 msgBox.style.display = 'block'; 471 var year = parseFloat(document.getElementById("app_date").innerHTML); 472 msgBox.innerHTML = "<span id ='app_date'>" + year + "</span> Querying database - please wait ......";473 new Ajax.Request("/patients/number_of_booked_patients?date=" + setdate ,{method:'get',onSuccess: function(transport){474 count = JSON.parse(transport.responseText) || ""; 475 if (count) { 476 msgBox.innerHTML = "<span id ='app_date'>" + year + "</span> Total number of booked patients on this day: " + count;477 }else{ 478 msgBox.innerHTML = "<span id ='app_date'>" + year + "</span> Total number of booked patients on this day: " + 0;479 } 480 }}); ...
DateUtil.js
Source:DateUtil.js
1/**2 * used in Core to convert the options into a volatile variable3 * 4 * @param {function} moment5 * @param {Object} body6 * @param {Array | Object} hiddenDates7 * @returns {number}8 */9exports.convertHiddenOptions = function(moment, body, hiddenDates) {10 if (hiddenDates && !Array.isArray(hiddenDates)) {11 return exports.convertHiddenOptions(moment, body, [hiddenDates])12 }13 body.hiddenDates = [];14 if (hiddenDates) {15 if (Array.isArray(hiddenDates) == true) {16 for (var i = 0; i < hiddenDates.length; i++) {17 if (hiddenDates[i].repeat === undefined) {18 var dateItem = {};19 dateItem.start = moment(hiddenDates[i].start).toDate().valueOf();20 dateItem.end = moment(hiddenDates[i].end).toDate().valueOf();21 body.hiddenDates.push(dateItem);22 }23 }24 body.hiddenDates.sort(function (a, b) {25 return a.start - b.start;26 }); // sort by start time27 }28 }29};30/**31 * create new entrees for the repeating hidden dates32 *33 * @param {function} moment34 * @param {Object} body35 * @param {Array | Object} hiddenDates36 * @returns {null}37 */38exports.updateHiddenDates = function (moment, body, hiddenDates) {39 if (hiddenDates && !Array.isArray(hiddenDates)) {40 return exports.updateHiddenDates(moment, body, [hiddenDates])41 }42 if (hiddenDates && body.domProps.centerContainer.width !== undefined) {43 exports.convertHiddenOptions(moment, body, hiddenDates);44 var start = moment(body.range.start);45 var end = moment(body.range.end);46 var totalRange = (body.range.end - body.range.start);47 var pixelTime = totalRange / body.domProps.centerContainer.width;48 for (var i = 0; i < hiddenDates.length; i++) {49 if (hiddenDates[i].repeat !== undefined) {50 var startDate = moment(hiddenDates[i].start);51 var endDate = moment(hiddenDates[i].end);52 if (startDate._d == "Invalid Date") {53 throw new Error("Supplied start date is not valid: " + hiddenDates[i].start);54 }55 if (endDate._d == "Invalid Date") {56 throw new Error("Supplied end date is not valid: " + hiddenDates[i].end);57 }58 var duration = endDate - startDate;59 if (duration >= 4 * pixelTime) {60 var offset = 0;61 var runUntil = end.clone();62 switch (hiddenDates[i].repeat) {63 case "daily": // case of time64 if (startDate.day() != endDate.day()) {65 offset = 1;66 }67 startDate.dayOfYear(start.dayOfYear());68 startDate.year(start.year());69 startDate.subtract(7,'days');70 endDate.dayOfYear(start.dayOfYear());71 endDate.year(start.year());72 endDate.subtract(7 - offset,'days');73 runUntil.add(1, 'weeks');74 break;75 case "weekly":76 var dayOffset = endDate.diff(startDate,'days');77 var day = startDate.day();78 // set the start date to the range.start79 startDate.date(start.date());80 startDate.month(start.month());81 startDate.year(start.year());82 endDate = startDate.clone();83 // force84 startDate.day(day);85 endDate.day(day);86 endDate.add(dayOffset,'days');87 startDate.subtract(1,'weeks');88 endDate.subtract(1,'weeks');89 runUntil.add(1, 'weeks');90 break;91 case "monthly":92 if (startDate.month() != endDate.month()) {93 offset = 1;94 }95 startDate.month(start.month());96 startDate.year(start.year());97 startDate.subtract(1,'months');98 endDate.month(start.month());99 endDate.year(start.year());100 endDate.subtract(1,'months');101 endDate.add(offset,'months');102 runUntil.add(1, 'months');103 break;104 case "yearly":105 if (startDate.year() != endDate.year()) {106 offset = 1;107 }108 startDate.year(start.year());109 startDate.subtract(1,'years');110 endDate.year(start.year());111 endDate.subtract(1,'years');112 endDate.add(offset,'years');113 runUntil.add(1, 'years');114 break;115 default:116 console.log("Wrong repeat format, allowed are: daily, weekly, monthly, yearly. Given:", hiddenDates[i].repeat);117 return;118 }119 while (startDate < runUntil) {120 body.hiddenDates.push({start: startDate.valueOf(), end: endDate.valueOf()});121 switch (hiddenDates[i].repeat) {122 case "daily":123 startDate.add(1, 'days');124 endDate.add(1, 'days');125 break;126 case "weekly":127 startDate.add(1, 'weeks');128 endDate.add(1, 'weeks');129 break;130 case "monthly":131 startDate.add(1, 'months');132 endDate.add(1, 'months');133 break;134 case "yearly":135 startDate.add(1, 'y');136 endDate.add(1, 'y');137 break;138 default:139 console.log("Wrong repeat format, allowed are: daily, weekly, monthly, yearly. Given:", hiddenDates[i].repeat);140 return;141 }142 }143 body.hiddenDates.push({start: startDate.valueOf(), end: endDate.valueOf()});144 }145 }146 }147 // remove duplicates, merge where possible148 exports.removeDuplicates(body);149 // ensure the new positions are not on hidden dates150 var startHidden = exports.isHidden(body.range.start, body.hiddenDates);151 var endHidden = exports.isHidden(body.range.end,body.hiddenDates);152 var rangeStart = body.range.start;153 var rangeEnd = body.range.end;154 if (startHidden.hidden == true) {rangeStart = body.range.startToFront == true ? startHidden.startDate - 1 : startHidden.endDate + 1;}155 if (endHidden.hidden == true) {rangeEnd = body.range.endToFront == true ? endHidden.startDate - 1 : endHidden.endDate + 1;}156 if (startHidden.hidden == true || endHidden.hidden == true) {157 body.range._applyRange(rangeStart, rangeEnd);158 }159 }160};161/**162 * remove duplicates from the hidden dates list. Duplicates are evil. They mess everything up.163 * Scales with N^2164 *165 * @param {Object} body166 */167exports.removeDuplicates = function(body) {168 var hiddenDates = body.hiddenDates;169 var safeDates = [];170 for (var i = 0; i < hiddenDates.length; i++) {171 for (var j = 0; j < hiddenDates.length; j++) {172 if (i != j && hiddenDates[j].remove != true && hiddenDates[i].remove != true) {173 // j inside i174 if (hiddenDates[j].start >= hiddenDates[i].start && hiddenDates[j].end <= hiddenDates[i].end) {175 hiddenDates[j].remove = true;176 }177 // j start inside i178 else if (hiddenDates[j].start >= hiddenDates[i].start && hiddenDates[j].start <= hiddenDates[i].end) {179 hiddenDates[i].end = hiddenDates[j].end;180 hiddenDates[j].remove = true;181 }182 // j end inside i183 else if (hiddenDates[j].end >= hiddenDates[i].start && hiddenDates[j].end <= hiddenDates[i].end) {184 hiddenDates[i].start = hiddenDates[j].start;185 hiddenDates[j].remove = true;186 }187 }188 }189 }190 for (i = 0; i < hiddenDates.length; i++) {191 if (hiddenDates[i].remove !== true) {192 safeDates.push(hiddenDates[i]);193 }194 }195 body.hiddenDates = safeDates;196 body.hiddenDates.sort(function (a, b) {197 return a.start - b.start;198 }); // sort by start time199};200exports.printDates = function(dates) {201 for (var i =0; i < dates.length; i++) {202 console.log(i, new Date(dates[i].start),new Date(dates[i].end), dates[i].start, dates[i].end, dates[i].remove);203 }204};205/**206 * Used in TimeStep to avoid the hidden times.207 * @param {function} moment208 * @param {TimeStep} timeStep209 * @param {Date} previousTime210 */211exports.stepOverHiddenDates = function(moment, timeStep, previousTime) {212 var stepInHidden = false;213 var currentValue = timeStep.current.valueOf();214 for (var i = 0; i < timeStep.hiddenDates.length; i++) {215 var startDate = timeStep.hiddenDates[i].start;216 var endDate = timeStep.hiddenDates[i].end;217 if (currentValue >= startDate && currentValue < endDate) {218 stepInHidden = true;219 break;220 }221 }222 if (stepInHidden == true && currentValue < timeStep._end.valueOf() && currentValue != previousTime) {223 var prevValue = moment(previousTime);224 var newValue = moment(endDate);225 //check if the next step should be major226 if (prevValue.year() != newValue.year()) {timeStep.switchedYear = true;}227 else if (prevValue.month() != newValue.month()) {timeStep.switchedMonth = true;}228 else if (prevValue.dayOfYear() != newValue.dayOfYear()) {timeStep.switchedDay = true;}229 timeStep.current = newValue;230 }231};232///**233// * Used in TimeStep to avoid the hidden times.234// * @param timeStep235// * @param previousTime236// */237//exports.checkFirstStep = function(timeStep) {238// var stepInHidden = false;239// var currentValue = timeStep.current.valueOf();240// for (var i = 0; i < timeStep.hiddenDates.length; i++) {241// var startDate = timeStep.hiddenDates[i].start;242// var endDate = timeStep.hiddenDates[i].end;243// if (currentValue >= startDate && currentValue < endDate) {244// stepInHidden = true;245// break;246// }247// }248//249// if (stepInHidden == true && currentValue <= timeStep._end.valueOf()) {250// var newValue = moment(endDate);251// timeStep.current = newValue.toDate();252// }253//};254/**255 * replaces the Core toScreen methods256 *257 * @param {vis.Core} Core258 * @param {Date} time259 * @param {number} width260 * @returns {number}261 */262exports.toScreen = function (Core, time, width) {263 var conversion;264 if (Core.body.hiddenDates.length == 0) {265 conversion = Core.range.conversion(width);266 return (time.valueOf() - conversion.offset) * conversion.scale;267 } else {268 var hidden = exports.isHidden(time, Core.body.hiddenDates);269 if (hidden.hidden == true) {270 time = hidden.startDate;271 }272 var duration = exports.getHiddenDurationBetween(Core.body.hiddenDates, Core.range.start, Core.range.end);273 if (time < Core.range.start) {274 conversion = Core.range.conversion(width, duration);275 var hiddenBeforeStart = exports.getHiddenDurationBeforeStart(Core.body.hiddenDates, time, conversion.offset);276 time = Core.options.moment(time).toDate().valueOf();277 time = time + hiddenBeforeStart;278 return -(conversion.offset - time.valueOf()) * conversion.scale;279 280 } else if (time > Core.range.end) {281 var rangeAfterEnd = {start: Core.range.start, end: time};282 time = exports.correctTimeForHidden(Core.options.moment, Core.body.hiddenDates, rangeAfterEnd, time);283 conversion = Core.range.conversion(width, duration);284 return (time.valueOf() - conversion.offset) * conversion.scale;285 } else {286 time = exports.correctTimeForHidden(Core.options.moment, Core.body.hiddenDates, Core.range, time);287 conversion = Core.range.conversion(width, duration);288 return (time.valueOf() - conversion.offset) * conversion.scale;289 }290 }291 };292/**293 * Replaces the core toTime methods294 *295 * @param {vis.Core} Core296 * @param {number} x297 * @param {number} width298 * @returns {Date}299 */300exports.toTime = function(Core, x, width) {301 if (Core.body.hiddenDates.length == 0) {302 var conversion = Core.range.conversion(width);303 return new Date(x / conversion.scale + conversion.offset);304 }305 else {306 var hiddenDuration = exports.getHiddenDurationBetween(Core.body.hiddenDates, Core.range.start, Core.range.end);307 var totalDuration = Core.range.end - Core.range.start - hiddenDuration;308 var partialDuration = totalDuration * x / width;309 var accumulatedHiddenDuration = exports.getAccumulatedHiddenDuration(Core.body.hiddenDates, Core.range, partialDuration);310 return new Date(accumulatedHiddenDuration + partialDuration + Core.range.start);311 }312};313/**314 * Support function315 *316 * @param {Array.<{start: Window.start, end: *}>} hiddenDates317 * @param {number} start318 * @param {number} end319 * @returns {number}320 */321exports.getHiddenDurationBetween = function(hiddenDates, start, end) {322 var duration = 0;323 for (var i = 0; i < hiddenDates.length; i++) {324 var startDate = hiddenDates[i].start;325 var endDate = hiddenDates[i].end;326 // if time after the cutout, and the327 if (startDate >= start && endDate < end) {328 duration += endDate - startDate;329 }330 }331 return duration;332};333/**334 * Support function335 *336 * @param {Array.<{start: Window.start, end: *}>} hiddenDates337 * @param {number} start338 * @param {number} end339 * @returns {number}340 */341exports.getHiddenDurationBeforeStart = function (hiddenDates, start, end) {342 var duration = 0;343 for (var i = 0; i < hiddenDates.length; i++) {344 var startDate = hiddenDates[i].start;345 var endDate = hiddenDates[i].end;346 if (startDate >= start && endDate <= end) {347 duration += endDate - startDate;348 }349 }350 return duration;351};352/**353 * Support function354 * @param {function} moment355 * @param {Array.<{start: Window.start, end: *}>} hiddenDates356 * @param {{start: number, end: number}} range357 * @param {Date} time358 * @returns {number}359 */360exports.correctTimeForHidden = function(moment, hiddenDates, range, time) {361 time = moment(time).toDate().valueOf();362 time -= exports.getHiddenDurationBefore(moment, hiddenDates,range,time);363 return time;364};365exports.getHiddenDurationBefore = function(moment, hiddenDates, range, time) {366 var timeOffset = 0;367 time = moment(time).toDate().valueOf();368 for (var i = 0; i < hiddenDates.length; i++) {369 var startDate = hiddenDates[i].start;370 var endDate = hiddenDates[i].end;371 // if time after the cutout, and the372 if (startDate >= range.start && endDate < range.end) {373 if (time >= endDate) {374 timeOffset += (endDate - startDate);375 }376 }377 }378 return timeOffset;379};380/**381 * sum the duration from start to finish, including the hidden duration,382 * until the required amount has been reached, return the accumulated hidden duration383 * @param {Array.<{start: Window.start, end: *}>} hiddenDates384 * @param {{start: number, end: number}} range385 * @param {number} [requiredDuration=0]386 * @returns {number}387 */388exports.getAccumulatedHiddenDuration = function(hiddenDates, range, requiredDuration) {389 var hiddenDuration = 0;390 var duration = 0;391 var previousPoint = range.start;392 //exports.printDates(hiddenDates)393 for (var i = 0; i < hiddenDates.length; i++) {394 var startDate = hiddenDates[i].start;395 var endDate = hiddenDates[i].end;396 // if time after the cutout, and the397 if (startDate >= range.start && endDate < range.end) {398 duration += startDate - previousPoint;399 previousPoint = endDate;400 if (duration >= requiredDuration) {401 break;402 }403 else {404 hiddenDuration += endDate - startDate;405 }406 }407 }408 return hiddenDuration;409};410/**411 * used to step over to either side of a hidden block. Correction is disabled on tablets, might be set to true412 * @param {Array.<{start: Window.start, end: *}>} hiddenDates413 * @param {Date} time414 * @param {number} direction415 * @param {boolean} correctionEnabled416 * @returns {Date|number}417 */418exports.snapAwayFromHidden = function(hiddenDates, time, direction, correctionEnabled) {419 var isHidden = exports.isHidden(time, hiddenDates);420 if (isHidden.hidden == true) {421 if (direction < 0) {422 if (correctionEnabled == true) {423 return isHidden.startDate - (isHidden.endDate - time) - 1;424 }425 else {426 return isHidden.startDate - 1;427 }428 }429 else {430 if (correctionEnabled == true) {431 return isHidden.endDate + (time - isHidden.startDate) + 1;432 }433 else {434 return isHidden.endDate + 1;435 }436 }437 }438 else {439 return time;440 }441};442/**443 * Check if a time is hidden444 *445 * @param {Date} time446 * @param {Array.<{start: Window.start, end: *}>} hiddenDates447 * @returns {{hidden: boolean, startDate: Window.start, endDate: *}}448 */449exports.isHidden = function(time, hiddenDates) {450 for (var i = 0; i < hiddenDates.length; i++) {451 var startDate = hiddenDates[i].start;452 var endDate = hiddenDates[i].end;453 if (time >= startDate && time < endDate) { // if the start is entering a hidden zone454 return {hidden: true, startDate: startDate, endDate: endDate};455 }456 }457 return {hidden: false, startDate: startDate, endDate: endDate};...
periodType.js
Source:periodType.js
1function PeriodType()2{3 var dateFormat = 'yyyy-MM-dd';4 var periodTypes = [];5 periodTypes['Daily'] = new DailyPeriodType( dateFormat );6 periodTypes['Weekly'] = new WeeklyPeriodType( dateFormat );7 periodTypes['Monthly'] = new MonthlyPeriodType( dateFormat );8 periodTypes['BiMonthly'] = new BiMonthlyPeriodType( dateFormat );9 periodTypes['Quarterly'] = new QuarterlyPeriodType( dateFormat );10 periodTypes['SixMonthly'] = new SixMonthlyPeriodType( dateFormat );11 periodTypes['SixMonthlyApril'] = new SixMonthlyAprilPeriodType( dateFormat );12 periodTypes['Yearly'] = new YearlyPeriodType( dateFormat );13 periodTypes['FinancialOct'] = new FinancialOctoberPeriodType( dateFormat );14 periodTypes['FinancialJuly'] = new FinancialJulyPeriodType( dateFormat );15 periodTypes['FinancialApril'] = new FinancialAprilPeriodType( dateFormat );16 this.get = function( key )17 {18 return periodTypes[key];19 };20 this.reverse = function( array )21 {22 var reversed = [];23 var i = 0;24 for ( var j = array.length - 1; j >= 0; j-- )25 {26 reversed[i++] = array[j];27 }28 return reversed;29 };30 this.filterFuturePeriods = function( periods )31 {32 var array = [];33 var i = 0;34 var now = new Date().getTime();35 for ( var j = 0; j < periods.length; j++ )36 {37 if ( $.date( periods[j]['endDate'], dateFormat ).date().getTime() <= now )38 {39 array[i++] = periods[j];40 }41 }42 return array;43 };44 45 this.filterFuturePeriodsExceptCurrent = function( periods )46 {47 var array = [];48 var i = 0;49 var now = new Date().getTime();50 for ( var j = 0; j < periods.length; j++ )51 {52 if ( $.date( periods[j]['startDate'], dateFormat ).date().getTime() <= now )53 {54 array[i++] = periods[j];55 }56 }57 return array;58 };59}60function DailyPeriodType( dateFormat )61{62 this.generatePeriods = function( offset )63 {64 var periods = [];65 var year = new Date().getFullYear() + offset;66 var startDate = $.date( year + '-01-01', dateFormat );67 var i = 0;68 while ( startDate.date().getFullYear() <= year )69 {70 var period = [];71 period['startDate'] = startDate.format( dateFormat );72 period['endDate'] = startDate.format( dateFormat );73 period['name'] = startDate.format( dateFormat );74 period['id'] = 'Daily_' + period['startDate'];75 period['iso'] = startDate.format( 'yyyyMMdd' );76 periods[i] = period;77 startDate.adjust( 'D', +1 );78 i++;79 }80 return periods;81 };82}83function WeeklyPeriodType( dateFormat )84{85 this.generatePeriods = function( offset )86 {87 var periods = [];88 var year = new Date().getFullYear() + offset;89 var startDate = $.date( year + '-01-01', dateFormat );90 var day = startDate.date().getDay();91 var i = 0;92 if ( day == 0 ) // Sunday (0), forward to Monday93 {94 startDate.adjust( 'D', +1 );95 }96 else if ( day <= 4 ) // Monday - Thursday, rewind to Monday97 {98 startDate.adjust( 'D', ( ( day - 1 ) * -1 ) );99 }100 else101 // Friday - Saturday, forward to Monday102 {103 startDate.adjust( 'D', ( 8 - day ) );104 }105 106 var endDate = startDate.clone().adjust( 'D', +6 );107 // Include all weeks where Thursday falls in same year108 109 while ( startDate.clone().adjust( 'D', 3 ).date().getFullYear() <= year )110 {111 var period = [];112 period['startDate'] = startDate.format( dateFormat );113 period['endDate'] = endDate.format( dateFormat );114 period['name'] = 'W' + ( i + 1 ) + ' - ' + startDate.format( dateFormat ) + " - " + endDate.format( dateFormat );115 period['id'] = 'Weekly_' + period['startDate'];116 period['iso'] = year + 'W' + ( i + 1 );117 periods[i] = period;118 startDate.adjust( 'D', +7 );119 endDate = startDate.clone().adjust( 'D', +6 );120 i++;121 }122 return periods;123 };124}125function MonthlyPeriodType( dateFormat )126{127 this.generatePeriods = function( offset )128 {129 var periods = [];130 var year = new Date().getFullYear() + offset;131 var startDate = $.date( year + '-01-01', dateFormat );132 var endDate = startDate.clone().adjust( 'M', +1 ).adjust( 'D', -1 );133 var i = 0;134 while ( startDate.date().getFullYear() == year )135 {136 var period = [];137 period['startDate'] = startDate.format( dateFormat );138 period['endDate'] = endDate.format( dateFormat );139 period['name'] = monthNames[i] + ' ' + year;140 period['id'] = 'Monthly_' + period['startDate'];141 period['iso'] = startDate.format( 'yyyyMM' );142 periods[i] = period;143 startDate.adjust( 'M', +1 );144 endDate = startDate.clone().adjust( 'M', +1 ).adjust( 'D', -1 );145 i++;146 }147 return periods;148 };149}150function BiMonthlyPeriodType( dateFormat )151{152 this.generatePeriods = function( offset )153 {154 var periods = [];155 var year = new Date().getFullYear() + offset;156 var startDate = $.date( year + '-01-01', dateFormat );157 var endDate = startDate.clone().adjust( 'M', +2 ).adjust( 'D', -1 );158 var i = 0;159 var j = 0;160 while ( startDate.date().getFullYear() == year )161 {162 var period = [];163 period['startDate'] = startDate.format( dateFormat );164 period['endDate'] = endDate.format( dateFormat );165 period['name'] = monthNames[i] + ' - ' + monthNames[i + 1] + ' ' + year;166 period['id'] = 'BiMonthly_' + period['startDate'];167 period['iso'] = startDate.format( 'yyyyMM' ) + 'B';168 periods[j] = period;169 startDate.adjust( 'M', +2 );170 endDate = startDate.clone().adjust( 'M', +2 ).adjust( 'D', -1 );171 i += 2;172 j++;173 }174 return periods;175 };176}177function QuarterlyPeriodType( dateFormat )178{179 this.generatePeriods = function( offset )180 {181 var periods = [];182 var year = new Date().getFullYear() + offset;183 var startDate = $.date( year + '-01-01', dateFormat );184 var endDate = startDate.clone().adjust( 'M', +3 ).adjust( 'D', -1 );185 var i = 0;186 var j = 0;187 while ( startDate.date().getFullYear() == year )188 {189 var period = [];190 period['startDate'] = startDate.format( dateFormat );191 period['endDate'] = endDate.format( dateFormat );192 period['name'] = monthNames[i] + ' - ' + monthNames[i + 2] + ' ' + year;193 period['id'] = 'Quarterly_' + period['startDate'];194 period['iso'] = year + 'Q' + ( j + 1 );195 periods[j] = period;196 startDate.adjust( 'M', +3 );197 endDate = startDate.clone().adjust( 'M', +3 ).adjust( 'D', -1 );198 i += 3;199 j++;200 }201 return periods;202 };203}204function SixMonthlyPeriodType( dateFormat )205{206 this.generatePeriods = function( offset )207 {208 var periods = [];209 var year = new Date().getFullYear() + offset;210 var period = [];211 period['startDate'] = year + '-01-01';212 period['endDate'] = year + '-06-30';213 period['name'] = monthNames[0] + ' - ' + monthNames[5] + ' ' + year;214 period['id'] = 'SixMonthly_' + period['startDate'];215 period['iso'] = year + 'S1';216 periods[0] = period;217 period = [];218 period['startDate'] = year + '-07-01';219 period['endDate'] = year + '-12-31';220 period['name'] = monthNames[6] + ' - ' + monthNames[11] + ' ' + year;221 period['id'] = 'SixMonthly_' + period['startDate'];222 period['iso'] = year + 'S2';223 periods[1] = period;224 return periods;225 };226}227function SixMonthlyAprilPeriodType( dateFormat )228{229 this.generatePeriods = function( offset )230 {231 var periods = [];232 var year = new Date().getFullYear() + offset;233 var period = [];234 period['startDate'] = year + '-04-01';235 period['endDate'] = year + '-09-30';236 period['name'] = monthNames[3] + ' - ' + monthNames[8] + ' ' + year;237 period['id'] = 'SixMonthlyApril_' + period['startDate'];238 period['iso'] = year + 'AprilS1';239 periods[0] = period;240 period = [];241 period['startDate'] = year + '-10-01';242 period['endDate'] = ( year + 1 ) + '-03-31';243 period['name'] = monthNames[9] + ' ' + year + ' - ' + monthNames[2] + ' ' + ( year + 1 );244 period['id'] = 'SixMonthlyApril_' + period['startDate'];245 period['iso'] = year + 'AprilS2';246 periods[1] = period;247 return periods;248 };249}250function YearlyPeriodType( dateFormat )251{252 this.generatePeriods = function( offset )253 {254 var periods = [];255 var year = new Date().getFullYear() + offset;256 var startDate = $.date( year + '-01-01', dateFormat ).adjust( 'Y', -5 );257 var endDate = startDate.clone().adjust( 'Y', +1 ).adjust( 'D', -1 );258 for ( var i = 0; i < 11; i++ )259 {260 var period = [];261 period['startDate'] = startDate.format( dateFormat );262 period['endDate'] = endDate.format( dateFormat );263 period['name'] = startDate.date().getFullYear();264 period['id'] = 'Yearly_' + period['startDate'];265 period['iso'] = startDate.date().getFullYear();266 periods[i] = period;267 startDate.adjust( 'Y', +1 );268 endDate = startDate.clone().adjust( 'Y', +1 ).adjust( 'D', -1 );269 }270 return periods;271 };272}273function FinancialOctoberPeriodType( dateFormat )274{275 this.generatePeriods = function( offset )276 {277 var periods = [];278 var year = new Date().getFullYear() + offset;279 var startDate = $.date( year + '-10-01', dateFormat ).adjust( 'Y', -5 );280 var endDate = startDate.clone().adjust( 'Y', +1 ).adjust( 'D', -1 );281 for ( var i = 0; i < 11; i++ )282 {283 var period = [];284 period['startDate'] = startDate.format( dateFormat );285 period['endDate'] = endDate.format( dateFormat );286 period['name'] = monthNames[9] + ' ' + startDate.date().getFullYear() + '-' + monthNames[8] + ' ' + (startDate.date().getFullYear() +1 );287 period['id'] = 'FinancialOct_' + period['startDate'];288 period['iso'] = startDate.date().getFullYear() + 'Oct';289 periods[i] = period;290 startDate.adjust( 'Y', +1 );291 endDate = startDate.clone().adjust( 'Y', +1 ).adjust( 'D', -1 );292 }293 return periods;294 };295}296function FinancialJulyPeriodType( dateFormat )297{298 this.generatePeriods = function( offset )299 {300 var periods = [];301 var year = new Date().getFullYear() + offset;302 var startDate = $.date( year + '-07-01', dateFormat ).adjust( 'Y', -5 );303 var endDate = startDate.clone().adjust( 'Y', +1 ).adjust( 'D', -1 );304 for ( var i = 0; i < 11; i++ )305 {306 var period = [];307 period['startDate'] = startDate.format( dateFormat );308 period['endDate'] = endDate.format( dateFormat );309 period['name'] = monthNames[6] + ' ' + startDate.date().getFullYear() + '-' + monthNames[5] + ' ' + (startDate.date().getFullYear() +1 );310 period['id'] = 'FinancialJuly_' + period['startDate'];311 period['iso'] = startDate.date().getFullYear() + 'July';312 periods[i] = period;313 startDate.adjust( 'Y', +1 );314 endDate = startDate.clone().adjust( 'Y', +1 ).adjust( 'D', -1 );315 }316 return periods;317 };318}319function FinancialAprilPeriodType( dateFormat )320{321 this.generatePeriods = function( offset )322 {323 var periods = [];324 var year = new Date().getFullYear() + offset;325 var startDate = $.date( year + '-04-01', dateFormat ).adjust( 'Y', -5 );326 var endDate = startDate.clone().adjust( 'Y', +1 ).adjust( 'D', -1 );327 for ( var i = 0; i < 11; i++ )328 {329 var period = [];330 period['startDate'] = startDate.format( dateFormat );331 period['endDate'] = endDate.format( dateFormat );332 period['name'] = monthNames[3] + ' ' + startDate.date().getFullYear() + '-' + monthNames[2] + ' ' + (startDate.date().getFullYear() +1 );333 period['id'] = 'FinancialApril_' + period['startDate'];334 period['iso'] = startDate.date().getFullYear() + 'April';335 periods[i] = period;336 startDate.adjust( 'Y', +1 );337 endDate = startDate.clone().adjust( 'Y', +1 ).adjust( 'D', -1 );338 }339 return periods;340 };...
Using AI Code Generation
1var BestDeal = require('./BestDeal.js');2var bestDeal = new BestDeal();3console.log(bestDeal.startDate());4var BestDeal = require('./BestDeal.js');5var bestDeal = new BestDeal();6console.log(bestDeal.endDate());7var BestDeal = require('./BestDeal.js');8var bestDeal = new BestDeal();9console.log(bestDeal.getDeal());10var BestDeal = require('./BestDeal.js');11var bestDeal = new BestDeal();12console.log(bestDeal.getDeal());13var BestDeal = require('./BestDeal.js');14var bestDeal = new BestDeal();15console.log(bestDeal.getDeal());16var BestDeal = require('./BestDeal.js');17var bestDeal = new BestDeal();18console.log(bestDeal.getDeal());19var BestDeal = require('./BestDeal.js');20var bestDeal = new BestDeal();21console.log(bestDeal.getDeal());22var BestDeal = require('./BestDeal.js');23var bestDeal = new BestDeal();24console.log(bestDeal.getDeal());25var BestDeal = require('./BestDeal.js');26var bestDeal = new BestDeal();27console.log(bestDeal.getDeal());28var BestDeal = require('./BestDeal.js');29var bestDeal = new BestDeal();30console.log(bestDeal.getDeal());31var BestDeal = require('./BestDeal.js');32var bestDeal = new BestDeal();33console.log(bestDeal.getDeal());34var BestDeal = require('./BestDeal.js');
Using AI Code Generation
1var BestBeforeDate = require('./BestBeforeDate.js');2var date1 = new BestBeforeDate(1, 1, 2000);3var date2 = new BestBeforeDate(2, 2, 2000);4var date3 = new BestBeforeDate(3, 3, 2000);5var date4 = new BestBeforeDate(4, 4, 2000);6var date5 = new BestBeforeDate(5, 5, 2000);7var date6 = new BestBeforeDate(6, 6, 2000);8var date7 = new BestBeforeDate(7, 7, 2000);9var date8 = new BestBeforeDate(8, 8, 2000);10var date9 = new BestBeforeDate(9, 9, 2000);11var date10 = new BestBeforeDate(10, 10, 2000);12var date11 = new BestBeforeDate(11, 11, 2000);13var date12 = new BestBeforeDate(12, 12, 2000);14var startDate = BestBeforeDate.startDate(date1, date2, date3, date4, date5, date6, date7, date8, date9, date10, date11, date12);15console.log(startDate);16var BestBeforeDate = function (month, day, year) {17 this.month = month;18 this.day = day;19 this.year = year;20};21BestBeforeDate.prototype.toString = function () {22 return this.month + "/" + this.day + "/" + this.year;23};24BestBeforeDate.prototype.compareTo = function (otherDate) {25 if (this.year < otherDate.year) {26 return -1;27 } else if (this.year > otherDate.year) {28 return 1;29 } else {30 if (this.month < otherDate.month) {31 return -1;32 } else if (this.month > otherDate.month) {33 return 1;34 } else {35 if (this.day < otherDate.day) {
Using AI Code Generation
1var BestBeforeDate = require('./BestBeforeDate.js');2var date = new BestBeforeDate(2016, 12, 12);3console.log(date.startDate());4var BestBeforeDate = function(year, month, day) {5 this.year = year;6 this.month = month;7 this.day = day;8};9BestBeforeDate.prototype.startDate = function() {10 var date = new Date(this.year, this.month, this.day);11 return date;12};13module.exports = BestBeforeDate;
Using AI Code Generation
1var BestBuy = require('./BestBuy.js');2var bestBuy = new BestBuy();3var startDate = new Date(2015, 7, 15);4var endDate = new Date(2015, 7, 16);5bestBuy.getBestBuy(startDate, endDate, function (err, result){6 if (err) {7 console.log(err);8 } else {9 console.log(result);10 }11});12var request = require('request');13var BestBuy = function () {14 this.getBestBuy = function (startDate, endDate, callback) {15 request(url, function (err, response, body) {16 if (err) {17 callback(err);18 } else {19 var data = JSON.parse(body);20 var bestBuy = 0;21 var bestBuyDate = null;22 for (var i = 0; i < data.Dates.length; i++) {23 var date = new Date(data.Dates[i]);24 if (date >= startDate && date <= endDate) {25 if (data.Elements[0].DataSeries.close.values[i] > bestBuy) {26 bestBuy = data.Elements[0].DataSeries.close.values[i];27 bestBuyDate = date;28 }29 }30 }31 var result = {32 };33 callback(null, result);34 }35 });36 };37};38module.exports = BestBuy;39var BestBuy = require('./BestBuy.js');40var bestBuy = new BestBuy();41var startDate = new Date(2015, 7, 15);42var endDate = new Date(2015, 7, 16);43bestBuy.getBestBuy(startDate, endDate, function (err, result){44 if (err) {45 console.log(err);46 } else {47 console.log(result);48 }49});
Using AI Code Generation
1var BestBeforeDate = require('./BestBeforeDate');2var date = new BestBeforeDate();3console.log("Best Before Date: " + date.startDate());4var BestBeforeDate = function() {5 this.startDate = function() {6 var today = new Date();7 var dd = today.getDate();8 var yyyy = today.getFullYear();9 if(dd<10){10 } 11 if(mm<10){12 } 13 var today = dd+'/'+mm+'/'+yyyy;14 return today;15 }16}17module.exports = BestBeforeDate;
Using AI Code Generation
1var bbd = new BestBeforeDate(1, 1, 2012);2var date = bbd.startDate();3print(date);4var bbd = new BestBeforeDate(1, 1, 2012);5var date = bbd.endDate();6print(date);7var bbd = new BestBeforeDate(1, 1, 2012);8var date = bbd.endDate();9print(date);10var bbd = new BestBeforeDate(1, 1, 2012);11var expired = bbd.isExpired();12print(expired);13var bbd = new BestBeforeDate(1, 1, 2000);14var expired = bbd.isExpired();15print(expired);16var bbd = new BestBeforeDate(1, 1, 2012);17var expired = bbd.isExpired();18print(expired);19var bbd = new BestBeforeDate(1, 1, 2000);20var expired = bbd.isExpired();21print(expired);22var bbd = new BestBeforeDate(
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!