Necesito disminuir/aumentar la fecha con una semana en cookie con botones hechos con JavaScript.
Funciones de las cookies:
function createCookie(name, value, days) { if (days) { var date = new Date(); date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); var expires = "; expires=" + date.toGMTString(); } else var expires = ""; document.cookie = name + "=" + value + expires + "; path=/;SameSite=None;Secure"; } function readCookie(name) { var nameEQ = name + "="; var ca = document.cookie.split(';'); for ( var i = 0; i < ca.length; i++) { var c = ca[i]; while (c.charAt(0) == ' ') c = c.substring(1, c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length); } return null; } function eraseCookie(name) { createCookie(name, "", -1); }Aquí está mi código que no funciona:
var dateCookie = new Date(); var dayCookie = dateCookie.getDate(); var monthCookie = dateCookie.getMonth() + 1; var yearCookie = dateCookie.getFullYear(); var weekCookie = jQuery.datepicker.iso8601Week(dateCookie); createCookie('day', readCookie('day') - 1, 30); createCookie('month', monthCookie, 30); createCookie('year', yearCookie, 30); createCookie('week', readCookie('week') - 1, 30); var year = readCookie('year'); var week = readCookie('week'); var d = new Date(readCookie('year')+"-"+readCookie('day')+"-"+readCookie('month')+" 00:00:00"); var w = d.getTime() + 604800000 * (week - 1); var n1 = new Date(w); var n2 = new Date(w + 518400000) console.log(n1); console.log(n2);Necesito hacer una publicación AJAX en mi servidor que publique las variables día, mes y año.
Necesito un equivalente de JavaScript para este código PHP:
<?php if (isset($_GET['month']) && isset($_GET['day']) && isset($_GET['year'])) { $month = (integer) $_GET['month']; $day = (integer) $_GET['day']; $year = (integer) $_GET['year']; } else { $week_day = date('w'); $week_start = date('j', strtotime('-'.$week_day.' days')); $week_end = date('j', strtotime('+'.(6-$week_day).' days')); $month = date('n'); $day = $week_start; $year = date('Y'); } $i = $day; $days = $day + 6; while ($i<=$days) { ?>Aquí hay un código de trabajo:
var dateCookie = new Date(); var dayCookie = dateCookie.getDate(); var monthCookie = dateCookie.getMonth() + 1; var yearCookie = dateCookie.getFullYear(); var weekCookie = jQuery.datepicker.iso8601Week(dateCookie); if (readCookie('week')==1) { createCookie('week', readCookie('week'), 1); } else { createCookie('week', readCookie('week') - 1, 1); } var year = readCookie('year'); var week = readCookie('week'); //var d = new Date(yearCookie+"-"+pad(dayCookie,2)+"-"+monthCookie); var curr = new Date(readCookie('year')+"-"+readCookie('month')+"-"+pad(readCookie('day'),2)); // get current date var first = curr.getDate() - curr.getDay(); // First day is the day of the month - the day of the week var last = first + 6; // last day is the first day + 6 var firstday = new Date(curr.setDate(first)).toUTCString(); var lastday = new Date(curr.setDate(last)).toUTCString(); var myCurrentDate=new Date(firstday); var myPastDate=new Date(myCurrentDate); myPastDate.setDate(myPastDate.getDate() - 7); //myPastDate is now 8 days in the past createCookie('day', myPastDate.getDate(), 1); createCookie('month', myPastDate.getMonth() + 1, 1); createCookie('year', myPastDate.getFullYear(), 1); Ext.getCmp('center-week').setTitle('Week ' + readCookie('week') + ', ' + readCookie('year')); <?php if (isset($_GET['month']) && isset($_GET['day']) && isset($_GET['year'])) { $month = (integer) $_GET['month']; $day = (integer) $_GET['day']; $year = (integer) $_GET['year']; } else { $week_day = date('w'); $week_start = date('j', strtotime('-'.$week_day.' days')); $week_end = date('j', strtotime('+'.(6-$week_day).' days')); $month = date('n'); $day = $week_start; $year = date('Y'); } $i = $day; $days = $day + 6; $y = 0; ?> var dayInt = parseInt(readCookie('day')); <?php while ($i<=$days) { ?> //alert(dayInt + parseInt(<?php echo $y; ?>)); Ext.getCmp('mytimesheetgrid<?php echo $i; ?>').setTitle(jQuery.datepicker.formatDate("DD, mm/dd/yy", new Date(readCookie('year')+"-"+readCookie('month')+"-"+pad(dayInt + parseInt(<?php echo $y; ?>),2)))); myworklogsstore<?php echo $i; ?>.load({params: { author: fullname, day: dayInt + parseInt(<?php echo $y; ?>), month: readCookie('month'), year: readCookie('year'), key: tasksCombo.getValue() }}); <?php $y++; $i++; } ?>