Home > Net >  How to create a date today and a date 1 year before
How to create a date today and a date 1 year before

Time:10-06

I have a WebService where I have to modify DATEFIN by today's date and DATEDEBUT by a date 1 year before.

Currently, I have this below

illustration

Here my dates are entered "manually", how I could create this correctly, please?

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

Here is my method getNews()

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

Thank you so much.

CodePudding user response:

You can do it like this if i understood the question correctly.

const date = new Date();
const year =  date.getFullYear()
const month = date.getMonth()
const day =  date.getDate()
var payload = {
        "HEADER": this.sh.getHeaderForRequest(),
        "SVM": SVM,
        "PERIODE": {
            "DATEDEBUT": (year-1) "-" month "-" day ,
            "DATEFIN": year "-" month "-" day
        },
        "LASTX": 0
    }

CodePudding user response:

Somethig like that:

var today = new Date();
var date = today.getFullYear() '-' (today.getMonth() 1) '-' today.getDate();

previous year:

var previousYear = today.getFullYear() - 1  '-' (today.getMonth() 1) '-' today.getDate();
  • Related