Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

122
Views
I need to decrease/increase date with one week in cookie with buttons made with JavaScript

I need to decrease/increase date with one week in cookie with buttons made with JavaScript.

Cookie functions:

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);
}

Here is my code that is not work:

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);

I need to makes an AJAX post to my server that post a variables day, month and year.

I need a JavaScript equivalent for this PHP code:

  <?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) {
        ?>
about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

Here is a working code:

                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++;
                    }
                ?>
about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!