Steps for generating a current date and a date one year in the past

In my WebService, I need to update the DATEFIN to today's date and the DATEDEBUT to a date that is one year prior.

Currently, the setup looks like this:

see image here

At the moment, I am manually inputting the dates. How can I automate this process correctly?

getUpdatedNews(SVM, last) {
    var payload = {
        "HEADER": this.sh.getHeaderForRequest(),
        "SVM": SVM,
        "PERIODE": {
            "DATEDEBUT": "2018-01-01",
            "DATEFIN": "2021-01-01"
        },
        "LASTX": 0
    }
    return this.http.post < any[] > (this.getBaseUrl() + `/WLOLARTL`, payload);
}

This is how my getUpdatedNews() method looks like:

getUpdatedNews() {
    return this.api.getNews(this.svm, 20)
        .pipe(
            map((response: {}) => {
                // this.getDetails();
                this.prepareData(response);
            })
        );
}

Any guidance on how to improve this would be highly appreciated. Thank you!

Answer №1

Here is an example:

let today = new Date();
let currentDate = today.getFullYear() + '-' + (today.getMonth()+1) + '-' + today.getDate();

Last year's date:

let previousYearDate = today.getFullYear() - 1 + '-' + (today.getMonth()+1) + '-' + today.getDate();

Answer №2

If I have understood the question correctly, you can achieve this like so:

const currentDate = new Date();
const currentYear =  currentDate.getFullYear()
const currentMonth = currentDate.getMonth()
const currentDay =  currentDate.getDate()
var dataPayload = {
        "HEADER": this.sh.getHeaderForRequest(),
        "SVM": SVM,
        "PERIOD": {
            "STARTDATE": (currentYear-1)+"-"+currentMonth+"-"+currentDay ,
            "ENDDATE": currentYear+"-"+currentMonth+"-"+currentDay
        },
        "LASTX": 0
    }

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

The email validation function is not functioning correctly when used in conjunction with the form submission

I'm currently working on my final project for my JavaScript class. I've run into a bit of a roadblock and could use some guidance. I am trying to capture input (all code must be done in JS) for an email address and validate it. If the email is va ...

Is it possible to replicate the functionality of "npm run x" without including a "scripts" entry?

If you want to run a node command within the "context" of your installed node_modules, one way to do it is by adding an entry in the scripts field of your package.json. For example: ... "scripts": { "test": "mocha --recursive test/**/*.js --compiler ...

What is the best way to remove an element from an array and add a new one?

Here is the array that I am working with: [ { "id": "z12", "val": "lu", "val2": "1", }, { "id": "z13", "val": "la", "val2" ...

Adding pictures to Cloudinary

My goal is to upload files directly to Cloudinary using nodejs. I have managed to achieve success when I manually set the path of the image I am uploading, as shown below: cloudinary.uploader.upload('./public/css/img/' + data.image) However, whe ...

Get help with managing your events using JQuery and PHP Calendar

Currently undertaking a project that involves the creation of a calendar, where by clicking on any date of the year, it will redirect to another page to input specific information. My issue lies in the fact that I have been unable to locate a basic calenda ...

I can see the JSON data printing to the console in Ionic 3, but it doesn't display on

I seem to be facing a challenge with passing the 'item' to my search function in my Ionic 3 app. Although I was able to successfully connect to a json data file and print objects to the console, I am encountering an error message on the page that ...

How can one generate an HTML element using a DOM "element"?

When extracting an element from an HTML page, one can utilize a DOM method like .getElementById(). This method provides a JavaScript object containing a comprehensive list of the element's properties. An example of this can be seen on a MDN documentat ...

Alert: User is currently engaging in typing activity, utilizing a streamlined RXJS approach

In my current project, I am in the process of adding a feature that shows when a user is typing in a multi-user chat room. Despite my limited understanding of RXJS, I managed to come up with the code snippet below which satisfies the basic requirements for ...

What is the process for publishing an npm package to a Nexus group repository?

After installing Nexus Repository Manager OSS version 3.2.1, I successfully ran it on my local machine. Setup Within Nexus, I have set up three NPM repositories: [PUBLIC] - acting as a proxy for the public npm registry [PRIVATE] - designated for my own ...

Leveraging AngularJS for retrieving the total number of elements in a specific sub array

I'm currently working on a to-do list application using Angular. My goal is to show the number of items marked as done from an array object of Lists. Each List contains a collection of to-dos, which are structured like this: [{listName: "ESSENTIALS", ...

Exploring the perfect blend of ReactJs Router Links with material-ui components, such as buttons

I am currently facing a challenge in integrating the functionality of react router with material ui components. For example, I have a scenario where I want to combine a router and a button. I attempted to merge them together and customize their style. In ...

Combining a Spring project and Angular 4 into a single repository with a single commit

I developed a Spring Boot app with an Angular 4 project. The file structure is set up as follows: (all JS files minified under the static directory in resources). https://i.sstatic.net/0ITCc.png Now I have two separate .gitignore files - one for the Java ...

Defined a data type using Typescript, however, the underlying Javascript code is operating with an incorrect data type

Recently delving into Typescript and following along with an educational video. Encountered a strange behavior that seems like a bug. For instance: const json = '{"x": 10, "y":10}'; const coordinates: { x: number; y: number } = JSON.parse(json); ...

How can I turn off credential suggestions in a React JS application?

Is there a way to disable managed credential suggestion on a React JS web page using a browser? I have tried using the autoComplete=off attribute and setting editable mode with an onFocus event, but the password suggestions are still appearing. Any help wo ...

The Typescript compiler has trouble locating the definition file for an npm package

Recently, I released an npm package that was written in typescript. However, I have been facing difficulties in getting the definition recognized by typescript (webback and vscode). The only workaround that has worked for me so far is creating a folder wit ...

The issue of assigning Ajax data to jQuery inputs with identical classes is not being resolved correctly

I am currently developing an Invoicing System where the Rate(Amount) value changes automatically when the Product(Item) is changed. The issue I am encountering is that when I change the first Product, all the other Product Rates change to the Rate of the ...

Installing and loading Node.js modules on the fly

I am currently developing a Node.js module called A that relies on another Node.js module, B, from NPM. With new versions of module B being released on NPM, I want my module A to automatically update to the latest version of module B (ensuring it always us ...

Transmit information between controllers during pageload in AngularJS without utilizing $rootscope

I currently have 2 controllers set up as follows: app.controller('ParentMenuController', function ($scope,MenuService) { $scope.contentLoaded = false; $scope.showButton = false; $scope.showButton = MenuService ...

Checking authentication globally using Vue.js

In the main blade file, I have the following code snippet: <script> window.App = {!! json_encode([ 'csrfToken' => csrf_token(), 'user' => Auth::user(), 'signedIn' => Auth::check() ...

Organize AngularJS ng-repeat using dictionary information

I'm dealing with a dictionary consisting of key-value pairs, which looks something like this: data = { "key1": 1000, "key2": 2000, "key3": 500 } My goal is to display this data in a table using AngularJS. One way I can achieve this is b ...