Display previous 2 weeks (using vue.js)

Hi, I am using a script in vue.js that currently shows the next 2 weeks, but I need to modify it to display the previous 2 weeks instead. Any suggestions? Thanks.

methods: {
    // Get all days without sunday:
    dates(index) {
      var week = new Array();
      let current = new Date();
      // Starting Monday not Sunday
      current.setDate((current.getDate() - current.getDay() +1));
      for (var i = 0; i < 13; i++) {
        let date = new Date(current);
        week.push(moment(date).format('DD.MM.YY'));
        current.setDate(current.getDate() +1);
      }
      return week[index];
    },

Answer №1

To travel back in time, you must subtract from the present date:

methods: {
    // Retrieve all days except Sunday:
    dates(index) {
      var week = new Array();
      let current = new Date();
      // Begin with Monday instead of Sunday
      current.setDate((current.getDate() - current.getDay() +1));
      for (var i = 0; i < 13; i++) {
        let date = new Date(current);
        week.push(moment(date).format('DD.MM.YY'));
        current.setDate(current.getDate() - 1); // <-- this line has been altered
      }
      return week[index];
    },

Answer №2

Give this code a shot:

function generateDates(index) {
      var week = new Array();
      let current = moment().subtract(1, 'days');
      for (var i = 0; i < 12; i++) {
        week.push(current.format('DD.MM.YY'));
        current = current.subtract(1, 'days')
      }
      console.log(week);
      return week[index];
}
console.log(generateDates(2));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.27.0/moment.min.js"></script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issues Encountered During Form Data Transmission via XHR

I require the ability to transfer files from one cloud service to another using Azure Functions running Node. In order to do this, I have installed the necessary packages (axios, form-data, xmlhttprequest) and am coding in VSCode. However, when dealing wi ...

"Transforming a static navbar to a fixed position causes the page to jump

Having some difficulty figuring this out. I'm working on a bootstrap navbar that transitions from static to fixed when the user scrolls past the logo at the top. Everything seems to be working fine, except for when the navbar reaches the top, it sudde ...

having difficulty obtaining the keyCode/key event while input event is triggered

Recently, I encountered a challenge where I attempted to log the event key during an input bound event on the input. Below is the code snippet of what I tried: <div id="app"> <input @input="onInput" @keyup="onKeyUp" type="text"> </div&g ...

What is the most effective way to showcase a list of image URLs in HTML with Vue?

Currently, I am working with an array called thumbnails that holds the paths to various images. My goal is to use masonry in Vue to arrange these images in a grid format, but I'm encountering some difficulties achieving the desired outcome. This is m ...

The object NativeModules from the 'react-native' requirement is currently empty

At the top of one of the node_modules utilized in my project, there is a line that reads: let RNRandomBytes = require('react-native').NativeModules.RNRandomBytes However, I've noticed that require('react-native').NativeModules ...

What is the correct method for dynamically importing framer-motion in NextJS?

I am currently utilizing framer-motion in my NextJS project. My goal is to import {motion} using Next's dynamic import method. However, I am encountering difficulties as it does not seem to function properly. import { motion } from "framer-motion ...

Sending an HTML form to an external ASP script

I am currently attempting to create an HTML form that can submit multiple variables to an external ASP file that generates a chat window. The given example I am working with can be viewed through this link, although it involves a two-step process. In the ...

The crash during compilation is triggered by the presence of react-table/react-table.css in the code

My code and tests are functioning properly, but I am facing a challenge with my react-table file. The react-table.js API specifies that in order to use their CSS file, I need to include import "react-table/react-table.css"; in my react-table.js file. Howev ...

What is the best way to append something to the textContent property of an HTML tag within a markup page?

While working on resolving an XSS vulnerability in the jqxGrid where the cell content is rendered as HTML, I encountered a scenario like this: <a href="javascript:alert('test');">Hello</a>. To address this issue, I am exploring ways t ...

Issue with Typescript not recognizing default properties on components

Can someone help me troubleshoot the issue I'm encountering in this code snippet: export type PackageLanguage = "de" | "en"; export interface ICookieConsentProps { language?: PackageLanguage ; } function CookieConsent({ langua ...

Set a class for the active menu item using jQuery based on the current window location

I have an HTML snippet here with a class called js-nav and a custom attribute named data-id. This data is crucial for determining the current sliding menu. <a class="nav-link js-nav" data-id="about" href="/#about">About</a> The approach I too ...

"Exploring the possibilities of mocking routes in Vue 3 with Vitest and

In my current project, I am attempting to simulate vue-router's route feature that is utilized in one of my components through route.path. I tried to replicate the process outlined in a guide found at this link, and here is what I ended up with: vi.mo ...

Reordering Columns in Apex Chart Heatmap

I'm a beginner with Apex charts and I'm currently working on implementing a Heatmap using the Apex chart in Vue.js. However, I've encountered an issue with updating the columns of the heatmap. I've attempted to reorder the options > ...

Implementing TestCafe with a Rails backend and utilizing fixtures data for testing

Currently, I am involved in a Rails project that utilizes RSpec for testing. We rely on various Rails fixture data to test our UI under different conditions. Recently, I came across TestCafe for handling functional UI testing, and I find it quite intrigui ...

Guide to reference points, current one is constantly nonexistent

As I work on hosting multiple dynamic pages, each with its own function to call at a specific time, I encounter an issue where the current ref is always null. This poses a challenge when trying to access the reference for each page. export default class Qu ...

Localization for Timeago JS Plugin

Currently, I have integrated the jQuery TimeAgo plugin into my project and included the following code snippet for localization in Portuguese: // Portuguese jQuery.timeago.settings.strings = { suffixAgo: "atrás", suffixFromNow: "a partir de agora", ...

Troubleshooting MODULE NOT FOUND Error in my First NodeJS Application: A guide to resolving common issues

Welcome to My NodeJS Learning Project! Embarking on my first NodeJS application journey, this project serves as a stepping stone for me to grasp the fundamentals. The goal is to create a basic CRUD web app for item management. Drawing from my experience i ...

Resolving circular errors in ESLint with Prettier

I'm in the process of creating a Vue component. However, I've encountered an error: Type annotations can only be used in TypeScript files. If I remove the type clause, then I encounter: Missing return type on function. Here's how the comp ...

Best practices for using Promises in NodeJS

I'm in the process of developing a library that builds on top of amqp.node (amqplib), focusing on simplifying RabbitMQ functionality for our specific project needs. This new library is designed to utilize Promises. So, for instance, when subscribing ...

Sorting prices in descending order using Expedia API

Is there a way to arrange prices in descending order using the response.hotelList[i].lowRate? The provided code is as follows: success: function (response) { var bil = 1; var hotel_size = response.hotelList.length; $('#listD ...